分享到WhatsApp

现在越来越多的APP支持国外用户,所以常常会遇到国际化的问题,其中就包含分享至Facebook、Twitter、WhatsApp的需求。

但是国内的一些分享集成SDK,如友盟分享,并没有经过大量数据的检测验证,也没有数据支持可以适应国外不同的网络环境。所以最保守的做法,就是直接集成第三方的SDK。

下面列举是几个比较常见的集成文档:

但是有一些APP,如WhatsApp,是没有官方的SDK,做起来会比较麻烦,也支持不了那么多的情况,在这里做一个补充分析:


1、添加白名单:
plist文件中添加代码段:

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

2、分享文本(或者是分享链接,WhatsApp聊天内部会自动识别文本是否为链接)

NSString *msg = @"YOUR MSG"; 
NSString *url = [NSString stringWithFormat:@"whatsapp://send?text=%@", [msg stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; 
NSURL *whatsappURL = [NSURL URLWithString: url]; 

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { 
  [[UIApplication sharedApplication] openURL: whatsappURL]; 
} else { 
  // Cannot open whatsapp 
}

3、分享图片:
WhatsApp没有实现图片或者其他多媒体的分享,所以只能通过UIDocumentInteractionController的方式,去到APP内打开文件的方式以实现分享。
具体实现文档请移至官方回应,这里我以图片分享为例:

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

参考文档

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 小时候喜欢坐大巴,特别是去远的地方,占据一个靠窗的位置,趴在窗户旁看窗外的风景。这个喜爱至今未变。 大巴飞驰,窗外...
    南风WindyNan阅读 387评论 0 3
  • 这篇小说短小精悍。它的情节是围绕一对小夫妻的生活、工作而写的琐事,好像一地鸡毛,题目很贴切。读后感觉作者对小说人物...
    腹有经纶气自华阅读 9,129评论 0 7
  • 致22岁的我 来到这里已经有这么长的时间,遇见了很多人和事,这些东西使我在快乐中成长。很幸运,能遇见大家。 ...
    懵懵星阅读 423评论 0 0
  • 人的一生会遇到两个人,一个温柔了岁月,一个惊艳了时光,那更重要的就是遇见,遇见是一件很美好的事情,我们每天都在期待...
    爱咬人的小仙女阅读 139评论 0 0
  • 一、马上能用的说话法 我们说话时可以选择把整体的内容的总结先说出来,然后再将内容的每一个点逐一说明。如,你这...
    奔跑的阿权阅读 215评论 2 2