简介
在不使用第三方的情况下,采用系统原生的方法,实现分享功能。有两种方式,一种是使用UIActivityViewController
,另外一种是使用Social框架中的SLComposeViewController
。二者的效分别果如下:
方式一的效果:
方式二的效果:
说明:方式一在选择完分享平台后,会来到图二中做进一步选择。也就是说,方式二其实是跳过了前面的选择面板,直接分享。
导入#import <Social/Social.h>
注意:从ios11开始,系统不允许跳过前面的选择面板直接来到图二(实际上是来到了但立刻被dismiss了),所以,ios11后只能采用第一种方式!
方式一介绍
// 点击分享后 执行
-(void)clickShare{
//分享的标题
NSString *textToShare = @"分享的标题";
//分享的图片
UIImage *imageToShare = [UIImage imageNamed:@"shareLOGO"];
//分享的url
NSURL *urlToShare = [NSURL URLWithString:@"http://www.."];
//如果想分享图片 就把图片添加进去 文字什么的同上
NSArray *activityItems = @[textToShare,imageToShare, urlToShare];
// 创建分享vc
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
// 设置不出现在活动的项目
activityVC.excludedActivityTypes =
@[UIActivityTypePrint,UIActivityTypeMessage,UIActivityTypeMail,
UIActivityTypePrint,UIActivityTypeAddToReadingList,UIActivityTypeOpenInIBooks,
UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
[vc presentViewController:activityVC animated:YES completion:nil];
// 分享之后的回调
activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
if (completed) {
STLog(@"completed");
//分享 成功
} else {
STLog(@"cancled");
//分享 取消
}
};
- excludedActivityTypes 的 可选项:
UIActivityTypeAirDrop,
UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeOpenInIBooks
方式二介绍
(iOS11之后 不适用)
// 点击分享后 执行
-(void)clickShare{
NSArray * items = self.SharePic; //分享图片 数组
// 确定分享服务类型(微信、QQ... ...)
NSString * serviceType = [self getServiceTypeWithType];
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType: serviceType];
for ( id obj in items){
if ([obj isKindOfClass:[UIImage class]]){
[composeVC addImage:(UIImage *)obj];
}else if ([obj isKindOfClass:[NSURL class]]){
[composeVC addURL:(NSURL *)obj];
}
}
// 添加要分享的文字
[composeVC setInitialText:@"分享"];
// 设置 分享操作回调
composeVC.completionHandler = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultDone) {
NSLog(@"----点击了发送");
}
else if (result == SLComposeViewControllerResultCancelled)
{
NSLog(@"----点击了取消");
}
};
@try{
// 弹出分享框
[self presentViewController:composeVC animated:YES completion:nil];
return YES;
}
@catch (NSException *exception){
NSLog(@"没有安装微信"); // 捕获 异常
return NO;
}
@finally {
}
return YES;
}
- 主流的serviceType可选项 :
serviceType = @"com.tencent.xin.sharetimeline";//微信
serviceType = @"com.tencent.mqq.ShareExtension";//QQ
serviceType = @"com.apple.share.SinaWeibo.post";//微博
serviceType = @"com.apple.share.TencentWeibo.post"
com.taobao.taobao4iphone.ShareExtension // 淘宝
com.apple.share.Flickr.post", // Flickr
com.laiwang.DingTalk.ShareExtension // 钉钉
com.apple.mobileslideshow.StreamShareService // iCloud
com.alipay.iphoneclient.ExtensionSchemeShare // 支付宝
com.apple.share.Facebook.post // Facebook
com.apple.share.Twitter.post // Twitter
com.apple.Health.HealthShareExtension", // 应该是健康管理
自定义
https://github.com/CainRun/ActivityControllerDemo
http://blog.csdn.net/qq350116542/article/details/52058232