iOS使用UIActivityViewController共享图像和URL到whatsapp

时间:2021-08-01 23:00:37

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

使用UIActivityViewController共享图像和URL对于facebook和gmail工作正常但对whatsapp无效。这是我使用的代码

- (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. 我错过了什么吗?

  3. Do i need to implement custom activity for whatsapp?
  4. 我需要为whatsapp实现自定义活动吗?

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

注意:在继续之前,我们需要从activityviewcontroller解决方案中找到这个隐藏的whatsapp

2 个解决方案

#1


7  

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

WhatsApp更新了不允许简单文本与图像或URL或文档共享的策略。

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

如果您尝试单独共享图像和URL,它将从阵列中获取最后一个对象并将共享该对象。

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

您还需要在iOS9的Plist文件中设置权限

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

#2


4  

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

您无法使用UIActivityViewController在WhatsApp*享图像和文本。只有你能发布的一件事。那是图像或文字。

#1


7  

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

WhatsApp更新了不允许简单文本与图像或URL或文档共享的策略。

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

如果您尝试单独共享图像和URL,它将从阵列中获取最后一个对象并将共享该对象。

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

您还需要在iOS9的Plist文件中设置权限

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

#2


4  

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

您无法使用UIActivityViewController在WhatsApp*享图像和文本。只有你能发布的一件事。那是图像或文字。