iOS共享图像和文本到WhatsApp

我已经GOOGLE了,并得到一些解决scheme,似乎唯一可能的方式是通过UIDocumentInteractionController 。 我find了只能共享文本的结果,也只find了共享图像的结果。

但是我想要的是分享两个

我知道这个问题可能是重复的,我只是想说清楚,这里是截图。

WhatsApp分享图片+文字

(这是从Android分享)

您可以使用UIActivityViewController共享图像,文本或URL。这里是一个小例子:

 NSString *textToShare = @"Enter your text to be shared"; UIImage * image = [UIImage imageNamed:@"imagename"]; NSArray *objectsToShare = @[textToShare, image]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil]; [self presentViewController:activityVC animated:YES completion:nil]; 

运行上面的代码,并select什么应用程序共享,如果你想你也可以通过其他媒体分享。 这是苹果的默认共享方法

通常没有提到的东西,用户实际上并不需要共享文本消息和图像。

如果您的文本包含URL,则whatsapp应用程序将尝试检索有关该URL的信息并显示预览

为了这个工作,你需要使URL符合开放graphics协议。 这基本上意味着URL需要在其DOM中包含相关预览数据的元标签

请在github上查看下面的项目

https://github.com/salesawagner/SharingWhatsApp

 typedef enum{ kSendText = 0, kSendImage, kSendTextWithImage, kSendAudio, kSendCancel } options; - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case kSendText: [[WASWhatsAppUtil getInstance] sendText:@"Text"]; break; case kSendImage: [[WASWhatsAppUtil getInstance] sendImage:[UIImage imageNamed:@"image.jpg"] inView:self.view]; break; case kSendTextWithImage: NSLog(@"Send text with image"); case kSendAudio: [[WASWhatsAppUtil getInstance] sendAudioinView:self.view]; break; default: NSLog(@"Cancel send"); break; } } 

好的,

据我所知在ios中是不可能的。 但我有一个替代解决scheme,你可以共享文本和图像两者。但这是一个棘手或我认为愚蠢的解决scheme。

  1. 创build一个视图,你可以把你的image.Write文本在该视图上,无论你想写。
  2. 在代码的帮助下拍摄该视图的屏幕截图。您将获得图像(添加文本和图像的视图图像)。
  3. 通过文件交互控制器分享这个图像。

这只是一个可能的解决scheme,如果你想文本和图像都。但如果你想与文本共享链接比。 。 。 。 。 。 。

你可以像这样使用UIDocumentInteractionController来达到这个目的:

 @property (retain) UIDocumentInteractionController * documentInteractionController; if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){ UIImage * iconImage = [UIImage imageNamed:@"YOUR IMAGE"]; NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"]; [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES]; _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]]; _documentInteractionController.UTI = @"net.whatsapp.image"; _documentInteractionController.delegate = self; [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES]; } else { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } 

检查此答案以供参考: https : //stackoverflow.com/a/20601051/2082569

你也可以看看Socialize SDK ,它也很容易使用,并与各种社交SDK集成在一起。 查看WhatsApp共享文档: http ://socialize.github.io/socialize-sdk-ios/whatsapp.html