现在越来越多的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];
}