11
votes

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

2
Did you make it work?Subham93
I have not excluded UIActivityTypeCopyToPasteboard. Atleast user can copy and paste it what's app if direct is not working.Subham93
sorry, doesn't work for facebook app too.Vashum
Not working, Can you pls help me to Post the image to Facebook?Anand Gautam

2 Answers

10
votes

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>
5
votes

You can't share image and text both in WhatsApp using UIActivityViewController. Only single thing you can post. That is image or text.