iOS sharing image and URL to whatsapp using UIActivityViewController

Vashum picture Vashum · Oct 6, 2015 · Viewed 10.2k times · Source

Sharing image and URL using UIActivityViewController works fine for facebook and gmail but didn't work for whatsapp. Here's the code i used

- (void)share {
    UIScreen *screen = [UIScreen mainScreen];
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContextWithOptions(screen.bounds.size, NO, 0);
    [keyWindow drawViewHierarchyInRect:keyWindow.bounds afterScreenUpdates:YES];
    UIImage *snapShotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImage *imageToShare = snapShotImage;

    NSURL *urlToShare = [NSURL URLWithString:@"http://google.com"];

    NSMutableArray *activityItems = [NSMutableArray arrayWithObjects:urlToShare, imageToShare, nil];

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    activityViewController.excludedActivityTypes = @[
                                                     UIActivityTypePrint,
                                                     UIActivityTypeCopyToPasteboard,
                                                     UIActivityTypeAssignToContact,
                                                     UIActivityTypeSaveToCameraRoll,
                                                     UIActivityTypeAddToReadingList,
                                                     UIActivityTypeAirDrop];


    [self presentViewController:activityViewController animated:YES completion:nil];
}
  1. Have I miss anything?
  2. Do i need to implement custom activity for whatsapp?

Note: Incase of 2 we need to find this hiding whatsapp from activityviewcontroller solution before proceeding

Answer

Jainam picture Jainam · May 14, 2016

WhatsApp has updated policies which doesn't allow simple text to be shared along with Image or URL or Document.

If you are trying to share image and URL separately, it will take the last object from the array and will share that.

You can send the image this way:

NSMutableArray *activityItems= [NSMutableArray arrayWithObjects:img, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop];    
[self presentViewController:activityViewController animated:YES completion:nil];

Also you would need to set permission in Plist file for iOS9

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