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

时间:2015-10-06 12:27:58

标签: ios objective-c uiactivityviewcontroller

使用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. 我有什么遗漏吗?
  2. 我是否需要为whatsapp实现自定义活动?
  3. 注意:在继续

    之前,我们需要找到这个hiding whatsapp from activityviewcontroller解决方案

2 个答案:

答案 0 :(得分:9)

WhatsApp已更新政策,不允许与图片,网址或文档共享简单文字。

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

您可以这样发送图片:

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];

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

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

答案 1 :(得分:4)

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