UIActivityViewController
NSString *textToShare = @"分享的标题";
UIImage *imageToShare = [UIImage imageNamed:@"test.jpg"];
NSURL *urlToShare = [NSURL URLWithString:@"http://www.baidu.com"];
//NSURL * urlToShare = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"zip"];
NSArray *activityItems = @[textToShare, imageToShare, urlToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
/*
不想显示的type
activityVC.excludedActivityTypes = @[];
点击的回调
activityVC.completionWithItemsHandler =
*/
[self presentViewController:activityVC animated:YES completion:nil];
Social Framework
#import <Social/Social.h>
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
if (composeVc == nil){
NSLog(@"未安装软件");
return;
}
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
NSLog(@"软件未配置登录信息");
return;
}
//添加分享的文字、图片、链接
[composeVc setInitialText:@"要分享的文本内容"];
[composeVc addImage:[UIImage imageNamed:@"choice_highlight"]];
[composeVc addURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[self presentViewController:composeVc animated:YES completion:nil];
//监听用户点击了取消还是发送
composeVc.completionHandler = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
} else {
}
};
系统只提供了下面几种分享平台:
SLServiceTypeTwitter;
SLServiceTypeFacebook;
SLServiceTypeSinaWeibo;
SLServiceTypeTencentWeibo;
SLServiceTypeLinkedIn;
实际可以根据id来分享到更多平台,如微信:
NSString *socialType = @"com.tencent.xin.sharetimeline";
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:socialType];
可以这么使用的原因是:iOS8之后系统推出的Share Extension,不是系统实现了微信分享,而是微信App的Share Extension提供了分享入口。所以这个不是私有API,可以放心使用,想获取手机中装的App的所有Share Extension的方法。只需调用下面一句代码。
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
看看控制台输出,手机内所有可用的Share Extension的bundle Id 都出来了。