Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS sharing image and URL to whatsapp using UIActivityViewController

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

like image 400
Vashum Avatar asked Oct 06 '15 12:10

Vashum


2 Answers

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

like image 111
shiju86.v Avatar answered Sep 22 '22 04:09

shiju86.v


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>
like image 33
Jainam Avatar answered Sep 23 '22 04:09

Jainam