2

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.

Drico
  • 1,284
  • 15
  • 33
  • 3
    Being rude and down-voting all answers won't help on getting more help. – redent84 Apr 11 '16 at 13:46
  • I downvoted them only because they don't fit the requirements of the issue (some don't share video, some show an action sheet). If someone else comes to this thread looking for an answer to the question, he will be disapointed when trying all the non downvoted answers. That's not intended to be rude :) – Drico Apr 11 '16 at 14:29
  • 1
    most probably you should ask this in WhatsApp support, do they provide any support for media like this `whatsapp://send?text=Hello World!` as all the current solution almost leads to UIDocumentInteractionController` – HardikDG Apr 12 '16 at 03:23
  • @Drico Did you find any solution for this? – Mohammad Zaid Pathan Dec 05 '16 at 07:19
  • @Drico TuneMoji is probably not sharing Video But GIF file which are considered as image file. – Mohammad Zaid Pathan Dec 05 '16 at 07:37
  • @ZaidPathan Unfortunatly not, still no solution for this one. And I don't succeed to send GIFs either (I mean not the way TuneMoji does) – Drico Dec 05 '16 at 11:17
  • Did you found any solution? – Coder ACJHP May 02 '19 at 10:27

4 Answers4

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'

HardikDG
  • 5,892
  • 2
  • 26
  • 55
1

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.");
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • I want to share a Video without presenting an ActionSheet. Your solution has nothing to do with this usecase. – Drico Apr 11 '16 at 13:40
  • Have you tried this code. This code doesn't show an action sheet...you just need to add this code in button's action method...(like sendToWhatsAppButtonTapped) and you need to pass video to whatsapp. – Mahendra Apr 11 '16 at 13:52
  • Your code will open WhatsApp, but without copying a video. – Drico Apr 11 '16 at 14:25
  • you said in question that you have already done by `UIActivityViewController` but you don't have idea by without opening `UIActivityViewController`, so I have given an example how to open whatsapp. – Mahendra Apr 11 '16 at 14:31
  • Ok, to be more precise, I want to create a button which opens what's app and shares a video, without presenting an ActionSheet to the user. – Drico Apr 11 '16 at 14:46
1

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.

redent84
  • 18,901
  • 4
  • 62
  • 85
-1

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)

Community
  • 1
  • 1
Witterquick
  • 6,048
  • 3
  • 26
  • 50