11

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

Community
  • 1
  • 1
Vashum
  • 853
  • 1
  • 10
  • 24

2 Answers2

10

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>
tenpn
  • 4,556
  • 5
  • 43
  • 63
Jainam
  • 116
  • 1
  • 5
  • Any option to share both image and text , currently can't share image and text both . – Jaywant Khedkar Jan 02 '17 at 11:35
  • @JaywantKhedkar Nope it is not still NOT possible to share image and text. However you can share an URL along with some text. May be you can share the image URL if you are getting the image from server along with some custom text that you want to share. – Jainam Jan 03 '17 at 11:54
  • Thank you , I will try with your solution hope so it will work for me . – Jaywant Khedkar Jan 04 '17 at 09:43
5

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

shiju86.v
  • 667
  • 5
  • 10