UIActivityViewController 使用大致使用步骤:
-
//原生分享
-(void)share{
//1.设定分享的内容,比如:
NSString *shareTitle = @"隐秘的角落";
UIImage *shareImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593694996876&di=cb27080027a6748f99ca4ac4a49fee29&imgtype=0&src=http%3A%2F%2Fimagepphcloud.thepaper.cn%2Fpph%2Fimage%2F74%2F157%2F824.jpg"]]];
NSURL *shareUrl = [NSURL URLWithString:@"https://www.iqiyi.com/v_2ffkws0bgr0.html?vfm=2008_aldbd&fv=p_02_01"];
NSArray *activityItems = @[shareTitle,
shareImage,
shareUrl]; // 必须要提供url 才会显示分享标签否则只显示图片
//2.创建分享的控制器
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
//3.设定不想显示的平台和功能
activityVC.excludedActivityTypes = [self excludetypes];
//4. 设置操作回调,用户点击 菜单按钮后事件执行完成会回调这个block
activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
if (completed) {
NSLog(@"分享成功");
}else{
NSLog(@"分享失败");
}
};
//5.显示分享菜单
[self presentViewController:activityVC animated:YES completion:nil];
}
-(NSArray *)excludetypes{
NSMutableArray *excludeTypeM = [NSMutableArray arrayWithArray:@[//UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop,
UIActivityTypeOpenInIBooks]];
if (@available(iOS 11.0, *)) {
[excludeTypeM addObject:UIActivityTypeMarkupAsPDF];
} else {
// Fallback on earlier versions
}
return excludeTypeM;
}
-
示意图:
下面我们再来看一个第三方的分享:WYShareSDK
如果是用cocoapods管理工具管理项目的话,只需要在Podfile里添加一句 pod 'WYShareSDK' 就可以了。
根据上面我们所说的,原生的分享不需要第三方平台的appKey和URL Schemes,那么第三方的分享就需要了,好了,下面我们就来做一些项目的准备工作:
【1】在Xcode中,选择你的工程设置项,选中TARGETS
一栏,在info
标签栏的URL type
添加URL scheme
为你在各大平台所注册应用程序的id (如下图所示)
【2】针对iOS9的适配.右击项目中的info.plist,然后用Open as -> Source Code的方式打开,增加如下代码:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqzoneopensdk</string>
</array>
<!-- 让iOS9 退回到以前可以支持http请求 -->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
【3】选中项目设置,在Build Settings
中的Other Linker Flags
中增加-fobj-arc
和-ObjC
这时编译一下你的项目,如果没有报错,恭喜你环境搭建成功
接下来.分享的代码使用 在AppDelegate
中注册 微信/QQ/和微博
在你需要分享的地方直接导入WYShareSDK.h
,调用其对应的方法分享到各个平台.例: