Share image/text through WhatsApp in an iOS app

Is it possible to share images, text or whatever you want through Whatsapp in a iOS app? I'm searching on google but I only found results talking about Android implementations.


Solution 1:

Is now possible in this way:

Send Text - Obj-C

NSString * msg = @"YOUR MSG";
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
} else {
    // Cannot open whatsapp
}

Send Text - Swift

let msg = "YOUR MSG"
let urlWhats = "whatsapp://send?text=\(msg)"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
    if let whatsappURL = NSURL(string: urlString) {
        if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
            UIApplication.sharedApplication().openURL(whatsappURL)
        } else {
            // Cannot open whatsapp
        }
    }
}

Send Image - Obj-C

-- in .h file

<UIDocumentInteractionControllerDelegate>

@property (retain) UIDocumentInteractionController * documentInteractionController;

-- in .m file

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){

    UIImage     * iconImage = [UIImage imageNamed:@"YOUR IMAGE"];
    NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

    _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    _documentInteractionController.UTI = @"net.whatsapp.image";
    _documentInteractionController.delegate = self;

    [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];


} else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

Send Image - Swift

let urlWhats = "whatsapp://app"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
    if let whatsappURL = NSURL(string: urlString) {

        if UIApplication.sharedApplication().canOpenURL(whatsappURL) {

            if let image = UIImage(named: "image") {
                if let imageData = UIImageJPEGRepresentation(image, 1.0) {
                    let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/whatsAppTmp.wai")
                    do {
                        try imageData.writeToURL(tempFile, options: .DataWritingAtomic)
                        self.documentInteractionController = UIDocumentInteractionController(URL: tempFile)
                        self.documentInteractionController.UTI = "net.whatsapp.image"
                        self.documentInteractionController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
                    } catch {
                        print(error)
                    }
                }
            }

        } else {
            // Cannot open whatsapp
        }
    }
}

Because a new security feature of iOS 9, you need add this lines on .plist file:

<key>LSApplicationQueriesSchemes</key>
 <array>
  <string>whatsapp</string>
 </array>

More information about url sheme: https://developer.apple.com/videos/play/wwdc2015-703/

I did not find a single solution for both. More information on http://www.whatsapp.com/faq/en/iphone/23559013

I made a small project to help some. https://github.com/salesawagner/SharingWhatsApp

Solution 2:

It is now possible. Have not tried yet though.

The latest release notes for whatsapp indicate that you can through the share extension:

WhatsApp accepts the following types of content:

  • text (UTI: public.plain-text)
  • photos (UTI: public.image)
  • videos (UTI: public.movie)
  • audio notes and music files (UTI: public.audio)
  • PDF documents (UTI: com.adobe.pdf)
  • contact cards (UTI: public.vcard)
  • web URLs (UTI: public.url)

Solution 3:

No this is not possible, whatsapp does not have any public API you can use.

Please note that this answer is correct for 2011 when there was no API for WhatsApp.

Now there is an api available for interacting with WhatsApp: http://www.whatsapp.com/faq/en/iphone/23559013

The Objective-C call to open one of these URLs is as follows:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

Solution 4:

This is the correct code for share link to whats app users.

NSString * url = [NSString stringWithFormat:@"http://video...bla..bla.."];
url =    (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) url, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));

  NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",url];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
} else {
    // can not share with whats app
}