I need to share videos from my app to WhatsApp. I currently can do this by using an UIActivityViewController
but the user experience is not good.
(The user presses a "send to WhatsApp" button and then has to select WhatsApp in the Action Sheet displayed by the UIActivityViewController
).
I know it's possible to open the WhatsApp application and to share videos. For example, the application TuneMoji
does it very well :
- The user presses the "send to WhatsApp"
- The WhatsApp application opens, asking for a destination user.
I'd like to do exactly the same.
Please, don't tell me to have a look at https://www.whatsapp.com/faq/en/iphone/23559013 , or to use a UIDocumentInteractionController
: I want to avoid presenting an ActionSheet to the user.
4 Answers
Answers 1
Try this on the button action:
func sendVideo(videoName: String, senderView: UIView) { if UIApplication.sharedApplication().canOpenURL(NSURL(string: "whatsapp://app")!) { let savePath: String = NSBundle.mainBundle().pathForResource(videoName, ofType: "mov")! let documentInteractionController = UIDocumentInteractionController(URL: NSURL.fileURLWithPath(savePath)) documentInteractionController.UTI = "net.whatsapp.movie" documentInteractionController.delegate = self documentInteractionController.presentOpenInMenuFromRect(CGRectMake(0, 0, 0, 0), inView: senderView, animated: true) } else { let alertController = UIAlertController(title: "Error occured", message: "WhatsApp is not installed on your device.", preferredStyle: .Alert) let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) alertController.addAction(defaultAction) presentViewController(alertController, animated: true, completion: nil) } }
For more information you can check ref: https://www.whatsapp.com/faq/en/iphone/23559013
Be sure to include WhatsApp URL scheme in your application's Info.plist under LSApplicationQueriesSchemes key if you want to query presence of WhatsApp on user's iPhone using 'canOpenURL'
Answers 2
You can open whatsapp like this.
NSURL *urlOfWhatsApp = [NSURL URLWithString:@"whatsapp://"]; if ([[UIApplication sharedApplication] canOpenURL:urlOfWhatsApp]) { //check app can open whatsapp or not. [[UIApplication sharedApplication] openURL:urlOfWhatsApp]; } else { NSLog(@"You device do not have whatsapp."); }
Answers 3
You can send text messages using WhatsApp URL Scheme like this:
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"]; if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { [[UIApplication sharedApplication] openURL: whatsappURL]; }
This will open the application and ask for destination to send Hello World!
.
To send images or videos, you can use the UIActivityViewController
like you already noticed, or use UIDocumentInteractionController
, that may be closer to what you are looking for:
_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL]; _documentController.delegate = self; _documentController.UTI = @"net.whatsapp.image"; [_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
To show only whatsapp in the application list, use the private whatsapp file extension or UTI:
Alternatively, if you want to show only WhatsApp in the application list (instead of WhatsApp plus any other public/*-conforming apps) you can specify a file of one of aforementioned types saved with the extension that is exclusive to WhatsApp:
- images - «.wai» which is of type net.whatsapp.image
- videos - «.wam» which is of type net.whatsapp.movie
- audio files - «.waa» which is of type net.whatsapp.audio
When triggered, WhatsApp will immediately present the user with the contact/group picker screen. The media will be automatically sent to a selected contact/group.
More information in this WhatsApp FAQ.
Answers 4
Here you have all you need:
https://www.whatsapp.com/faq/en/iphone/23559013
Edit: and you can see example at this SO to send text / images directly to whats app -
Share image/text through WhatsApp in an iOS app
(see Wagner Sales' answer)
0 comments:
Post a Comment