From 6983e68da47d18dbff98db22a8035100e779b2bf Mon Sep 17 00:00:00 2001 From: Jigar Fumakiya Date: Wed, 18 Aug 2021 23:19:21 +0530 Subject: [PATCH] [IOS Whatsapp ] able to share direct messages with phone number. --- .idea/libraries/Dart_SDK.xml | 40 ++-- example/.flutter-plugins-dependencies | 1 + example/ios/Flutter/AppFrameworkInfo.plist | 2 +- .../ios/Flutter/flutter_export_environment.sh | 9 +- example/ios/Runner.xcodeproj/project.pbxproj | 6 +- example/lib/main.dart | 8 +- ios/Classes/SwiftFlutterShareMePlugin.swift | 212 +++++++++--------- lib/flutter_share_me.dart | 4 +- 8 files changed, 144 insertions(+), 138 deletions(-) create mode 100644 example/.flutter-plugins-dependencies diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index 6068110..d9b2670 100644 --- a/.idea/libraries/Dart_SDK.xml +++ b/.idea/libraries/Dart_SDK.xml @@ -1,26 +1,26 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies new file mode 100644 index 0000000..6c78ab0 --- /dev/null +++ b/example/.flutter-plugins-dependencies @@ -0,0 +1 @@ +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_share_me","path":"/Users/jigafumakiya/Documents/Jigar/plugin/open_flutter_share_me/flutter_share_me/","dependencies":[]},{"name":"image_picker","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.8.3+2/","dependencies":[]},{"name":"path_provider","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.2/","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_plugin_android_lifecycle-2.0.2/","dependencies":[]},{"name":"flutter_share_me","path":"/Users/jigafumakiya/Documents/Jigar/plugin/open_flutter_share_me/flutter_share_me/","dependencies":[]},{"name":"image_picker","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.8.3+2/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"path_provider","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.2/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-2.0.2/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-2.0.2/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-2.0.3/","dependencies":[]}],"web":[{"name":"image_picker_for_web","path":"/Users/jigafumakiya/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker_for_web-2.1.2/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"flutter_share_me","dependencies":[]},{"name":"image_picker","dependencies":["flutter_plugin_android_lifecycle","image_picker_for_web"]},{"name":"image_picker_for_web","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2021-08-18 23:12:26.787440","version":"2.5.0-7.0.pre.130"} \ No newline at end of file diff --git a/example/ios/Flutter/AppFrameworkInfo.plist b/example/ios/Flutter/AppFrameworkInfo.plist index 9367d48..8d4492f 100644 --- a/example/ios/Flutter/AppFrameworkInfo.plist +++ b/example/ios/Flutter/AppFrameworkInfo.plist @@ -21,6 +21,6 @@ CFBundleVersion 1.0 MinimumOSVersion - 8.0 + 9.0 diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index 3f8d372..735c2bc 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -1,15 +1,14 @@ #!/bin/sh # This is a generated file; do not edit or check into version control. -export "FLUTTER_ROOT=/Users/lizhuoyuan/Development/flutter" -export "FLUTTER_APPLICATION_PATH=/Users/lizhuoyuan/Development/Project/flutter_share_me/example" +export "FLUTTER_ROOT=/Users/jigafumakiya/Documents/FlutterSDK/flutter" +export "FLUTTER_APPLICATION_PATH=/Users/jigafumakiya/Documents/Jigar/plugin/open_flutter_share_me/flutter_share_me/example" export "COCOAPODS_PARALLEL_CODE_SIGN=true" -export "FLUTTER_TARGET=/Users/lizhuoyuan/Development/Project/flutter_share_me/example/lib/main.dart" +export "FLUTTER_TARGET=/Users/jigafumakiya/Documents/Jigar/plugin/open_flutter_share_me/flutter_share_me/example/lib/main.dart" export "FLUTTER_BUILD_DIR=build" -export "SYMROOT=${SOURCE_ROOT}/../build/ios" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=100" export "DART_DEFINES=Zmx1dHRlci5pbnNwZWN0b3Iuc3RydWN0dXJlZEVycm9ycz10cnVl,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==" export "DART_OBFUSCATION=false" export "TRACK_WIDGET_CREATION=true" export "TREE_SHAKE_ICONS=false" -export "PACKAGE_CONFIG=/Users/lizhuoyuan/Development/Project/flutter_share_me/example/.dart_tool/package_config.json" +export "PACKAGE_CONFIG=/Users/jigafumakiya/Documents/Jigar/plugin/open_flutter_share_me/flutter_share_me/example/.dart_tool/package_config.json" diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 9c02033..535e8e4 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -355,7 +355,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; - DEVELOPMENT_TEAM = HW92UC978N; + DEVELOPMENT_TEAM = CS942AL422; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -484,7 +484,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; - DEVELOPMENT_TEAM = HW92UC978N; + DEVELOPMENT_TEAM = CS942AL422; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -507,7 +507,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; - DEVELOPMENT_TEAM = HW92UC978N; + DEVELOPMENT_TEAM = CS942AL422; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/example/lib/main.dart b/example/lib/main.dart index d9442fe..0d0f451 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -16,7 +16,6 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String msg = 'hello,this is my github:https://github.com/lizhuoyuan'; File? file; ImagePicker picker = ImagePicker(); @@ -94,7 +93,12 @@ class _MyAppState extends State { response = await flutterShareMe.shareToTwitter(url: url, msg: msg); break; case Share.whatsapp: - response = await flutterShareMe.shareToWhatsApp(msg: msg, imagePath: file!.path); + if(file!=null){ + response = await flutterShareMe.shareToWhatsApp(msg: msg, imagePath: file!.path); + }else{ + response = await flutterShareMe.shareToWhatsApp(msg: msg); + } + break; case Share.whatsapp_business: response = await flutterShareMe.shareToWhatsApp(msg: msg); diff --git a/ios/Classes/SwiftFlutterShareMePlugin.swift b/ios/Classes/SwiftFlutterShareMePlugin.swift index 6d57e75..2fefffd 100644 --- a/ios/Classes/SwiftFlutterShareMePlugin.swift +++ b/ios/Classes/SwiftFlutterShareMePlugin.swift @@ -17,23 +17,23 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate if args!["url"] as! String == "" { // if don't pass url then pass blank so if can strat normal whatsapp - shareWhatsApp(message: args!["msg"] as! String,imageUrl: "",result: result) + shareWhatsApp(message: args!["msg"] as! String,imageUrl: "",phoneNumber: args!["phoneNumber"] as! String,result: result) }else{ // if user pass url then use that - shareWhatsApp(message: args!["msg"] as! String,imageUrl: args!["url"] as! String,result: result) + shareWhatsApp(message: args!["msg"] as! String,imageUrl: args!["url"] as! String,phoneNumber: args!["phoneNumber"] as! String,result: result) } } else if(call.method.elementsEqual("shareWhatsApp4Biz")){ let args = call.arguments as? Dictionary - + if args!["url"]as! String == "" { // if don't pass url then pass blank so if can strat normal whatsapp shareWhatsApp4Biz(message: args!["msg"] as! String, result: result) }else{ // if user pass url then use that // wil open share sheet and user can select open for there. - shareWhatsApp(message: args!["msg"] as! String,imageUrl: args!["url"] as! String,result: result) + shareWhatsApp(message: args!["msg"] as! String,imageUrl: args!["url"] as! String,phoneNumber: args!["phoneNumber"] as! String,result: result) } } else if(call.method.elementsEqual("shareFacebook")){ @@ -51,12 +51,12 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate } - func shareWhatsApp(message:String, imageUrl:String,result: @escaping FlutterResult) { + func shareWhatsApp(message:String, imageUrl:String,phoneNumber:String,result: @escaping FlutterResult) { // @ for ios // we can't set both if you pass image then text will ignore var whatsURL = "" if(imageUrl==""){ - whatsURL = "whatsapp://send?text=\(message)" + whatsURL = "whatsapp://send?phone=\(phoneNumber)&text=\(message)" }else{ whatsURL = "whatsapp://app" } @@ -79,7 +79,7 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate // if user share image then text will ignore because as of now there is no way to have both let image = UIImage(named: imageUrl) if let imageData = image!.jpegData(compressionQuality: 1.0) { - // we we want to share image then image need to be in docuemnt folder so create new image in document folder and share that image. + // we we want to share image then image need to be in docuemnt folder so create new image in document folder and share that image. let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai") do{ try imageData.write(to: tempFile, options: .atomic) @@ -126,122 +126,122 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate // }else{ // result(FlutterError(code: "Not found", message: "This not image path", details: "Check image path.")); // } - + + + } + else + { + result(FlutterError(code: "Not found", message: "WhatsApp is not found", details: "WhatsApp not intalled or Check url scheme.")); + } } - else - { - result(FlutterError(code: "Not found", message: "WhatsApp is not found", details: "WhatsApp not intalled or Check url scheme.")); - } - -} - -func shareWhatsApp4Biz(message:String, result: @escaping FlutterResult) { - let whatsApp = "https://wa.me/?text=\(message)" - var characterSet = CharacterSet.urlQueryAllowed - characterSet.insert(charactersIn: "?&") - let whatsAppURL = NSURL(string: whatsApp.addingPercentEncoding(withAllowedCharacters: characterSet)!) - if UIApplication.shared.canOpenURL(whatsAppURL! as URL) - { - result("Sucess"); - UIApplication.shared.openURL(whatsAppURL! as URL) + func shareWhatsApp4Biz(message:String, result: @escaping FlutterResult) { + let whatsApp = "https://wa.me/?text=\(message)" + var characterSet = CharacterSet.urlQueryAllowed + characterSet.insert(charactersIn: "?&") + + let whatsAppURL = NSURL(string: whatsApp.addingPercentEncoding(withAllowedCharacters: characterSet)!) + if UIApplication.shared.canOpenURL(whatsAppURL! as URL) + { + result("Sucess"); + UIApplication.shared.openURL(whatsAppURL! as URL) + } + else + { + result(FlutterError(code: "Not found", message: "WhatsAppBusiness is not found", details: "WhatsAppBusiness not intalled or Check url scheme.")); + } } - else - { - result(FlutterError(code: "Not found", message: "WhatsAppBusiness is not found", details: "WhatsAppBusiness not intalled or Check url scheme.")); + // share twitter + // params + // @ map conting meesage and url + + func sharefacebook(message:Dictionary, result: @escaping FlutterResult) { + let viewController = UIApplication.shared.delegate?.window??.rootViewController + let shareDialog=ShareDialog() + let shareContent = ShareLinkContent() + shareContent.contentURL = URL.init(string: message["url"] as! String)! + shareContent.quote = message["msg"] as? String + shareDialog.mode = .automatic + ShareDialog(fromViewController: viewController, content: shareContent, delegate: self).show() + result("Sucess") + } -} -// share twitter -// params -// @ map conting meesage and url - -func sharefacebook(message:Dictionary, result: @escaping FlutterResult) { - let viewController = UIApplication.shared.delegate?.window??.rootViewController - let shareDialog=ShareDialog() - let shareContent = ShareLinkContent() - shareContent.contentURL = URL.init(string: message["url"] as! String)! - shareContent.quote = message["msg"] as? String - shareDialog.mode = .automatic - ShareDialog(fromViewController: viewController, content: shareContent, delegate: self).show() - result("Sucess") -} - - -// share twitter params -// @ message -// @ url - -func shareTwitter(message:String,url:String, result: @escaping FlutterResult) { - let urlstring = url - let twitterUrl = "twitter://post?message=\(message)" - let urlTextEscaped = urlstring.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) - let url = URL(string: urlTextEscaped ?? "") + // share twitter params + // @ message + // @ url - let urlWithLink = twitterUrl + url!.absoluteString - - let escapedShareString = urlWithLink.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! - // cast to an url - let urlschme = URL(string: escapedShareString) - // open in safari - do { - if UIApplication.shared.canOpenURL(urlschme! as URL){ - UIApplication.shared.openURL(urlschme!) - result("Sucess") - }else{ + func shareTwitter(message:String,url:String, result: @escaping FlutterResult) { + let urlstring = url + let twitterUrl = "twitter://post?message=\(message)" + + let urlTextEscaped = urlstring.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) + let url = URL(string: urlTextEscaped ?? "") + + let urlWithLink = twitterUrl + url!.absoluteString + + let escapedShareString = urlWithLink.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! + // cast to an url + let urlschme = URL(string: escapedShareString) + // open in safari + do { + if UIApplication.shared.canOpenURL(urlschme! as URL){ + UIApplication.shared.openURL(urlschme!) + result("Sucess") + }else{ + result(FlutterError(code: "Not found", message: "Twitter is not found", details: "Twitter not intalled or Check url scheme.")); + + } + } catch { result(FlutterError(code: "Not found", message: "Twitter is not found", details: "Twitter not intalled or Check url scheme.")); - } - } catch { - result(FlutterError(code: "Not found", message: "Twitter is not found", details: "Twitter not intalled or Check url scheme.")); + + + } - -} - - -func systemShare(message:String,result: @escaping FlutterResult) { - // find the root view controller - let viewController = UIApplication.shared.delegate?.window??.rootViewController - - - // set up activity view controller - - - // Here is the message for for sharing - let objectsToShare = [message] as [Any] - let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) - /// if want to exlude anything then will add it for future support. - // activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop, UIActivity.ActivityType.addToReadingList] - if UIDevice.current.userInterfaceIdiom == .pad { - if let popup = activityVC.popoverPresentationController { - popup.sourceView = viewController?.view - popup.sourceRect = CGRect(x: (viewController?.view.frame.size.width)! / 2, y: (viewController?.view.frame.size.height)! / 4, width: 0, height: 0) + func systemShare(message:String,result: @escaping FlutterResult) { + // find the root view controller + let viewController = UIApplication.shared.delegate?.window??.rootViewController + + + // set up activity view controller + + + // Here is the message for for sharing + let objectsToShare = [message] as [Any] + let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) + /// if want to exlude anything then will add it for future support. + // activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop, UIActivity.ActivityType.addToReadingList] + if UIDevice.current.userInterfaceIdiom == .pad { + if let popup = activityVC.popoverPresentationController { + popup.sourceView = viewController?.view + popup.sourceRect = CGRect(x: (viewController?.view.frame.size.width)! / 2, y: (viewController?.view.frame.size.height)! / 4, width: 0, height: 0) + } } + viewController!.present(activityVC, animated: true, completion: nil) + result("Sucess"); + + } - viewController!.present(activityVC, animated: true, completion: nil) - result("Sucess"); -} - - -///Facebook delegate methods - -public func sharer(_ sharer: Sharing, didCompleteWithResults results: [String : Any]) { - print("Share: Success") + ///Facebook delegate methods -} - -public func sharer(_ sharer: Sharing, didFailWithError error: Error) { - print("Share: Fail") + public func sharer(_ sharer: Sharing, didCompleteWithResults results: [String : Any]) { + print("Share: Success") + + } -} - -public func sharerDidCancel(_ sharer: Sharing) { - print("Share: Cancel") -} + public func sharer(_ sharer: Sharing, didFailWithError error: Error) { + print("Share: Fail") + + } + + public func sharerDidCancel(_ sharer: Sharing) { + print("Share: Cancel") + } } diff --git a/lib/flutter_share_me.dart b/lib/flutter_share_me.dart index 73ec0a1..550d282 100644 --- a/lib/flutter_share_me.dart +++ b/lib/flutter_share_me.dart @@ -33,12 +33,14 @@ class FlutterShareMe { ///share to WhatsApp /// [imagePath] is local image + /// [phoneNumber] enter phone number with counry code /// For ios /// If include image then text params will be ingored as there is no current way in IOS share both at the same. - Future shareToWhatsApp({String msg = '', String imagePath = ''}) async { + Future shareToWhatsApp({String msg = '', String imagePath = '',String phoneNumber=''}) async { final Map arguments = Map(); arguments.putIfAbsent('msg', () => msg); arguments.putIfAbsent('url', () => imagePath); + arguments.putIfAbsent('phoneNumber', () => phoneNumber); dynamic result; try { result = await _channel.invokeMethod('shareWhatsApp', arguments);