官方文档解释:https://faq.whatsapp.com/zh_cn/iphone/23559013 其实就是调用系统分享,whatsapp是Facebook旗下的一款app,没有像Facebook一样提供分享的SDK API.
whatsapp分享链接时附带的图片分享不出去.
链接分享使用canOpenURL/openURL 打开第三方APP。(需要在.plist文件中添加whatsapp白名单)
分享图片使用系统的UIDocumentInteractionController共享文件。不可跳过系统选择面板
// 将图片/文件转换为NSURL NSURL *tempFile = “需要分享的file转换 ”; // 使用 UIDocumentInteractionController 共享文件 UIDocumentInteractionController* _docControll = [UIDocumentInteractionController interactionControllerWithURL:tempFile]; _docControll.UTI = UTIWithWhatsAppType(type); // 设置分享的类型,分享的类型见下图 _docControll.delegate = self; [_docControll presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]; text (UTI: public.plain-text) photos (UTI: public.image) videos (UTI: public.movie) audio notes and music files (UTI: public.audio) PDF documents (UTI: com.adobe.pdf) contact cards (UTI: public.vcard) web URLs (UTI: public.url) images - «.wai» which is of type net.whatsapp.image videos - «.wam» which is of type net.whatsapp.movie audio files - «.waa» which is of type net.whatsapp.audio也可以使用UIActivityViewController.
UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; NSArray *items = @[image]; // 可以添加其他信息,例如 链接、 分享消息 // 自定义ViewController 继承 UIActivityViewController,可以自定义方向 MyActivityController *activityVC = [[MyActivityController alloc]initWithActivityItems:activityItems applicationActivities:nil]; g // prensent 出系统controller [self.vc presentViewController:activityVC animated:YES completion:nil]; // 接收回调 UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) { if (completed){ NSLog(@"completed"); } }; activityVC.completionWithItemsHandler = myBlock;当遇到 多渠道分享,例如:Facebook&WhatsApp 分享,Facebook无法使用系统分享,只能调用FBSDK,而系统分享选择面板无法监听点击效果。此时需要自定义分享面板。可使用UITableView