iOS将图像和文本共享给WhatsApp

Js *_*Lim 36 share ios uidocumentinteraction whatsapp

我用谷歌搜索,并得到一些解决方案,似乎唯一可行的方法是通过UIDocumentInteractionController.我找到了只能共享文本的结果,也找到了仅共享图像的结果.

但我要的是份额BOTH.

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

WhatsApp分享图片+文字

(这是从Android共享的)

Gau*_*raw 19

您可以使用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];
Run Code Online (Sandbox Code Playgroud)

如果您希望也可以通过其他媒介分享,请运行上面的代码并选择要分享的应用程序.这是苹果的默认共享方法

  • 以这种方式"textToShare"字符串不分享什么应用...所以,这对我不起作用... (2认同)
  • 它的工作正常,但图像只分享不内容. (2认同)

atu*_*tri -5

您可以使用 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];
}
Run Code Online (Sandbox Code Playgroud)

检查此答案以供参考:/sf/answers/1442073601/

您还可以查看Socialize SDK,它也非常易于使用,并且与各种社交 SDK 集成。检查此文档以了解 Whatsapp 共享:http://socialize.github.io/socialize-sdk-ios/whatsapp.html

  • 我没有看到设置消息的属性。上面的代码我已经测试过很多次了,只能分享图片。我的问题是 **如何在一次共享中共享两者** (6认同)