开发工具:Xcode
集成方式:手动导入SDK或者Pod集成
SDK版本支持:SDK支持Xcode 9.1.0, iOS8.0+及以上版本
集成前准备
注册账号
使用ShareSDK之前,需要先在MobTech官网注册开发者账号,并获取AppKey和AppSecret,详情可以点击查看创建应用流程
分享到第三方平台,需要现在对应第三方平台申请平台的AppKey,详情可以点击查看注册应用流程
ShareSDK流程图
ShareSDK集成
手动集成
获取ShareSDK
在 MobService下载中心 下载最新版本SDK
选中ShareSDK,选择需要的平台,然后点击“保存配置”:
温馨提示: 各平台的右边有个问号,将鼠标放置上去会弹出该平台的简单说明
选中所需要的平台后,点击“下载”按钮,就会下载好SDK
下载下来的SDK结构
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code>SDK目录结构说明
SDK:
Required:
MOBFoundation.framework (基础工具库)
ShareSDK:
ShareSDK.framework (主业务实现)
Support:
Required:
ShareSDK.bundle (ShareSDK资源文件)
ShareSDKConnector.framework(v4.<span style="color:#6897bb">2.0</span>版本后已废弃)
PlatformConnector:
... (各个平台独立逻辑依赖库,只需要集成需要的平台即可)
PlatformSDK:
... (各个平台的SDK文件,不集成默认走去SDK业务逻辑,目前支持大部分主流
平台,如QQ 微博 微信等)
Optional:(可选)
ShareSDKUI.bundle (分享UI的资源文件)
ShareSDKUI.framework (分享UI库)
ShareSDKConfigFile.framework(xml配置文件形式分享依赖库)
ShareSDKConfigFile.bundle (xml配置文件形式分享资源文件)
ShareSDKExtension.framework(功能的扩展框架插件。(第三方平台检测、
一键分享、截屏分享、摇一摇分享等相关功能)
ShareSDKLink.framework(闭环分享库)
</code></span></span>
注意:导入的时候,需要将整个SDK资源文件导入到项目里
手动导入SDK
将上面获取到的SDK,直接将整个SDK资源文件拖进项目里,如下图:
并且勾选以下3个选项
在点击Finish,完成导入。
添加依赖库
点击“+”号,并在弹框里输入以下依赖库,进行添加
必要依赖库:
- libc++.tbd
- libz.tbd
- libsqlite3.tbd
社交平台依赖库(根据自己需求添加):
苹果登录依赖库:
- AuthenticationServices.framework
Facebook依赖库:
- AuthenticationServices.framework
- SafariServices.framework
- Accelerate.framework
新浪微博
- ImageIO.framework
- Photos.framework
- AssetsLibrary.framework
- Photos.framework
美拍
- AssetsLibrary.framework
Xcode配置
(1)在项目的Build Settings中的Other Linker Flags双击添加”-ObjC” ,注意大小写
(2)XCode15编译的时候需要在项目的Build Settings中的Other Linker Flags双击添加”-ld64”
CocoaPods集成
请参考 Pod集成
配置MobTech的appkey和serect
配置ShareSDK的AppKey和AppSecret,键分别为 MOBAppKey 和 MOBAppSecret ,值为之前在MobTech官网开发者后台申请的appkey和appSecret(注意: 配置之后保存好,配置好后看项目的Info选项里有没)
接口调用
回传用户隐私授权结果(uploadPrivacyPermissionStatus)
为保证您的App在集成MobSDK之后能够满足工信部相关合规要求,您应确保App安装首次冷启动且取得用户阅读您《隐私政策》授权之后,调用MobSDK.uploadPrivacyPermissionStatus回传隐私协议授权结果,反之,如果用户不同意您App《隐私政策》授权,则不能调用uploadPrivacyPermissionStatus回传隐私协议授权结果,相关隐私声明请参考ShareSDK隐私政策。
- 通用方案
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-obj-c"><span style="color:grey">/**
上传隐私协议授权状态
@param isAgree 是否同意(用户授权后的结果)
@param OnResult 执行回调结果,可为nil (注意业务逻辑不要依赖于这个success结果,建议业务逻辑在调用这个接口之后来写)
*/</span>
+ (<span style="color:#ffc66d">void</span>)uploadPrivacyPermissionStatus:(<span style="color:#ffc66d">BOOL</span>)isAgree
onResult:(<span style="color:#ffc66d">void</span> (^_Nullable)(<span style="color:#ffc66d">BOOL</span> success))<span style="color:#cc7832">handler</span>;
</code></span></span>
- 增加App数据采集主动控制器方案
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-obj-c"><span style="color:grey">/**
上传隐私协议授权状态
@param isAgree 是否同意(用户授权后的结果)
@param privacyDataDelegate 隐私数据配置
@param handler 回掉
*/</span>
+ (<span style="color:#cc7832">void</span>)uploadPrivacyPermissionStatus:(BOOL)isAgree
privacyDataDelegate:(id<MOBFoundationPrivacyDelegate> _Nullable)privacyDataDelegate
onResult:(<span style="color:#cc7832">void</span> (^_Nullable)(BOOL success))<span style="color:#cc7832">handler</span>;
</code></span></span>
示例代码
注:uploadPrivacyPermissionStatus是MobSDK的业务起点。调用位置开发者可以自己指定,只需在使用SDK功能之前调用即可,强烈建议开发者在终端用户点击应用隐私协议弹窗同意按钮后调用。
App数据采集主动控制器用来限制MobSDK采集并提供可选的数据维度和扩展功能。因相关信息的不收集将会对其对应的功能造成影响,请开发者结合业务实际需要进行合理配置。MobPush扩展业务功能设置可参考: https://www.mob.com/wiki/detailed?wiki=667&id=14 ,其它App数据采集主动控制器的配置方法可参考:https://www.mob.com/wiki/detailed?wiki=675&id=714
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-obj-c"><span style="color:#bbb529">#import <MOBFoundation/MobSDK+Privacy.h></span>
MobCustomController *privacyDataServie = [MobCustomController new];
[MobSDK uploadPrivacyPermissionStatus:<span style="color:#6897bb">YES</span> privacyDataDelegate:privacyDataServie onResult:^(BOOL success) {
}];
<span style="color:grey">// 或者</span>
[MobSDK uploadPrivacyPermissionStatus:<span style="color:#6897bb">YES</span> onResult:^(BOOL success) {
}];
</code></span></span>
导入头文件并进行第三方平台初始化(registPlatforms)
通常都是启动的时候在 application:didFinishLaunchingWithOptions:中添加初始化方法,也可保证注册在ShareSDK的业务执行之前即可。 这里提供几个主平台的初始化,其他平台可以参考demo里的MOBShareSDKHelper.m文件调用 点击这里
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code><span style="color:grey">/**
ShareSDK 平台注册方法
@param importHandler 用于设置各平台注册信息,也可在mob管理后台进行注册
*/</span>
+ (<span style="color:#cc7832">void</span>)<span style="color:#6a8759">registPlatforms:</span>(<span style="color:#cc7832">void</span>(^)(SSDKRegister *platformsRegister))importHandler;
</code></span></span>
示例代码
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-objective-c"><span style="color:#bbb529">#import <ShareSDK/ShareSDK.h></span>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
<span style="color:grey">//QQ</span>
[platformsRegister setupQQWithAppId:<span style="color:#6a8759">@"100371282"</span> appkey:<span style="color:#6a8759">@"aed9b0303e3ed1e27bae87c33761161d"</span> enableUniversalLink:<span style="color:#6897bb">YES</span> universalLink:<span style="color:#6a8759">@"https://70imc.share2dlink.com/qq_conn/100371282"</span>];
<span style="color:grey">//更新到4.3.3或者以上版本,微信初始化需要使用以下初始化</span>
[platformsRegister setupWeChatWithAppId:<span style="color:#6a8759">@"wx617c77c82218ea2c"</span> appSecret:<span style="color:#6a8759">@"c7253e5289986cf4c4c74d1ccc185fb1"</span> universalLink:<span style="color:#6a8759">@"https://70imc.share2dlink.com/"</span>];
<span style="color:grey">//新浪</span>
[platformsRegister setupSinaWeiboWithAppkey:<span style="color:#6a8759">@"568898243"</span> appSecret:<span style="color:#6a8759">@"38a4f8204cc784f81f9f0daaf31e02e3"</span> redirectUrl: <span style="color:#6a8759">@"http://www.sharesdk.cn"</span> universalLink:<span style="color:#6a8759">@"https://70imc.share2dlink.com/"</span>];
}];
<span style="color:#cc7832">return</span> <span style="color:#6897bb">YES</span>;
}
</code></span></span>
分享(showShareActionSheet)
在项目需要分享的按钮事件里加以下接口:
注意:根据自己的需求调用接口
弹出菜单分享
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code>/**
* 设置分享参数
*
* <span style="color:#bbb529">@param</span> text 文本
* <span style="color:#bbb529">@param</span> images 图片集合,传入参数可以为单张图片信息,也可以为一个NSArray,数组元素可以为UIImage、NSString(图片路径)、NSURL(图片路径)、SSDKImage。如: @<span style="color:#6a8759">"http://www.mob.com/images/logo_black.png"</span> 或 @[@<span style="color:#6a8759">"http://www.mob.com/images/logo_black.png"</span>]
* <span style="color:#bbb529">@param</span> url 网页路径/应用路径
* <span style="color:#bbb529">@param</span> title 标题
* <span style="color:#bbb529">@param</span> <span style="color:#cc7832">type</span> 分享类型
*/
- (void)SSDKSetupShareParamsByText:(NSString *)text
images:(id)images
url:(NSURL *)url
title:(NSString *)title
<span style="color:#cc7832">type</span>:(SSDKContentType)<span style="color:#cc7832">type</span>;
</code></span></span>
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code>/**
显示分享菜单
@param view 要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图
@param items 菜单项,如果传入nil,则显示已集成的平台列表
@param shareParams 分享内容参数
@param configuration 分享菜单的设置
@param <span style="color:#cc7832">state</span>ChangedHandler 分享状态变更事件
@return 分享菜单控制器
*/
+ (id)showShareActionSheet:(UIView *)view
customItems:(NSArray *)items
shareParams:(NSMutableDictionary *)shareParams
sheetConfiguration:(SSUIShareSheetConfiguration *)configuration
<span style="color:#cc7832">on</span>StateChanged:(SSUIShareStateChangedHandler)<span style="color:#cc7832">state</span>ChangedHandler;
</code></span></span>
示例代码
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-objective-c"><span style="color:#bbb529">#import <ShareSDK/ShareSDK.h></span>
<span style="color:#bbb529">#import <ShareSDKUI/ShareSDK+SSUI.h></span>
NSMutableDictionary * params = [NSMutableDictionary dictionary];
[params SSDKSetupShareParamsByText:<span style="color:#6a8759">@"test"</span>
images:<span style="color:#6a8759">@"http://download.sdk.mob.com/web/images/2019/07/30/14/1564468183056/750_750_65.12.png"</span>
url:[NSURL URLWithString:<span style="color:#6a8759">@"http://www.mob.com/"</span>]
title:<span style="color:#6a8759">@"title"</span>
type:SSDKContentTypeAuto];
[ShareSDK showShareActionSheet:<span style="color:#6897bb">nil</span> <span style="color:grey">//(第一个参数要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,在ipad中要想弹出我们的分享菜单,这个参数必须要传值,可以传自己分享按钮的对象,或者可以创建一个小的view对象去传,传值与否不影响iphone显示) </span>
customItems:<span style="color:#6897bb">nil</span>
shareParams:params
sheetConfiguration:<span style="color:#6897bb">nil</span>
onStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType,NSDictionary *userData,SSDKContentEntity *contentEntity,NSError *error,BOOL end)
{
<span style="color:#cc7832">switch</span> (state) {
<span style="color:#cc7832">case</span> SSDKResponseStateSuccess:
NSLog(<span style="color:#6a8759">@"成功"</span>);<span style="color:grey">//成功</span>
<span style="color:#cc7832">break</span>;
<span style="color:#cc7832">case</span> SSDKResponseStateFail:
{
NSLog(<span style="color:#6a8759">@"--%@"</span>,error.description);<span style="color:grey">//失败</span>
<span style="color:#cc7832">break</span>;
}
<span style="color:#cc7832">case</span> SSDKResponseStateCancel:
<span style="color:#cc7832">break</span>;
<span style="color:#cc7832">default</span>:
<span style="color:#cc7832">break</span>;
}
}];
</code></span></span>
直接分享
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code>/**
分享内容
@param platformType 平台类型
@param parameters 分享参数
@param <span style="color:#cc7832">state</span>ChangedHandler 状态变更回调处理
@return 会话
*/
+ (SSDKSession *)share:(SSDKPlatformType)platformType
parameters:(NSMutableDictionary *)parameters
<span style="color:#cc7832">on</span>StateChanged:(SSDKShareStateChangedHandler)<span style="color:#cc7832">state</span>ChangedHandler;
</code></span></span>
示例代码
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-objective-c"><span style="color:#bbb529">#import <ShareSDK/ShareSDK.h></span>
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params SSDKSetupShareParamsByText:<span style="color:#6a8759">@"test"</span>
images:[UIImage imageNamed:<span style="color:#6a8759">@"shareImg.png"</span>]
url:[NSURL URLWithString:<span style="color:#6a8759">@"http://www.mob.com/"</span>]
title:<span style="color:#6a8759">@"title"</span>
type:SSDKContentTypeAuto];
[ShareSDK share:SSDKPlatformTypeWechat
parameters:params
onStateChanged:^(SSDKResponseState state, NSDictionary *userData,
SSDKContentEntity *contentEntity, NSError *error) {
<span style="color:#cc7832">switch</span> (state) {
<span style="color:#cc7832">case</span> SSDKResponseStateSuccess:
NSLog(<span style="color:#6a8759">@"成功"</span>);<span style="color:grey">//成功</span>
<span style="color:#cc7832">break</span>;
<span style="color:#cc7832">case</span> SSDKResponseStateFail:
{
NSLog(<span style="color:#6a8759">@"--%@"</span>,error.description);
<span style="color:grey">//失败</span>
<span style="color:#cc7832">break</span>;
}
<span style="color:#cc7832">case</span> SSDKResponseStateCancel:
<span style="color:grey">//取消</span>
<span style="color:#cc7832">break</span>;
<span style="color:#cc7832">default</span>:
<span style="color:#cc7832">break</span>;
}
}];
</code></span></span>
登录(authorize)
每次都会向第三方平台请求授权
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code>/**
授权
@param platformType 平台类型
@param settings 授权设置,接受scopes属性设置,如新浪微博关注官方微博:@{@<span style="color:#6a8759">"scopes"</span> : @[@<span style="color:#6a8759">"follow_app_official_microblog"</span>]},类似“follow_app_official_microblog”这些字段是各个社交平台提供的。QQ平台如设置二维码授权,添加字段为QQAuthType:@{@<span style="color:#6a8759">"QQAuthType"</span>:@<span style="color:#6897bb">1</span>}, <span style="color:#6897bb">0</span>为网页账号密码登录授权
@param <span style="color:#cc7832">state</span>ChangedHandler 授权状态变更回调处理
@return 会话id
*/
+ (SSDKSession *)authorize:(SSDKPlatformType)platformType
settings:(NSDictionary *)settings
<span style="color:#cc7832">on</span>StateChanged:(SSDKAuthorizeStateChangedHandler)<span style="color:#cc7832">state</span>ChangedHandler;
</code></span></span>
示例代码
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-objective-c"><span style="color:#bbb529">#import <ShareSDK/ShareSDK.h></span>
[ShareSDK authorize:SSDKPlatformTypeWechat
settings:<span style="color:#6897bb">nil</span>
onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
<span style="color:#cc7832">switch</span> (state) {
<span style="color:#cc7832">case</span> SSDKResponseStateSuccess:
NSLog(<span style="color:#6a8759">@"%@"</span>,[user.credential rawData]);
<span style="color:#cc7832">break</span>;
<span style="color:#cc7832">case</span> SSDKResponseStateFail:
{
NSLog(<span style="color:#6a8759">@"--%@"</span>,error.description);
<span style="color:grey">//失败</span>
<span style="color:#cc7832">break</span>;
}
<span style="color:#cc7832">case</span> SSDKResponseStateCancel:
<span style="color:grey">//用户取消授权</span>
<span style="color:#cc7832">break</span>;
<span style="color:#cc7832">default</span>:
<span style="color:#cc7832">break</span>;
}
}];
</code></span></span>
第三方平台的相关配置
配置URL Scheme
打开项目的Info选项,然后选择URL Types,添加对应平台的URL Scheme配置,如下图:
以下是主要平台URL Scheme配置参考,需要看其他的平台请参考全平台配置说明
平台 | 配置URL Scheme 格式 | 举例 | 备注 |
---|---|---|---|
新浪微博 | wb+微博初始化的appKey | 如AppKey:568898243 最后配置:wb568898243 | |
QQ/Qzone | tencent+appID ,QQ+APPID的十六进制 | 如appID:100371282 最后配置:tencent100371282,QQ05FB8B52 注意appid十进制转十六进制,可以在[这里] 转换,如果appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写 | |
微信 | 微信的appid | 如appID:wx617c77c82218ea2c,最后配置:wx617c77c82218ea2c | |
fb+在facebook注册得到的ApiKey | 如ApiKey:107704292745179 最后配置:fb107704292745179 | ||
twitterkit-在twitter注册得到的ConsumerKey | 如ConsumerKey:viOnkeLpHBKs6KXV7MPpeGyzE 最后配置:twitterkit-viOnkeLpHBKs6KXV7MPpeGyzE |
配置白名单
1.在项目的info.plist中添加LSApplicationQueriesSchemes,类型为Array
2.然后给它添加一个需要支持的项目,类型为字符串类型:
以下是主要平台白名单配置参考,需要看其他的平台请参考 白名单全平台配置说明
平台名称 | OpenURL白名单说明 |
---|---|
新浪微博 | sinaweibo |
sinaweibohd | |
sinaweibosso | |
sinaweibohdsso | |
weibosdk | |
weibosdk2.5 | |
weibosdk3.3 | |
微信 | |
weixin | |
weixinULAPI | |
mqqOpensdkSSoLogin | |
mqqopensdkapiV2 | |
mqqopensdkapiV3 | |
wtloginmqq2 | |
mqq | |
mqqapi | |
timapi | |
mqqopensdkminiapp | |
mqqopensdknopasteboard | |
mqqopensdkapiV4 | |
QZONE | mqzoneopensdk |
mqzoneopensdkapi | |
mqzoneopensdkapi19 | |
mqzoneopensdkapiV2 | |
mqqOpensdkSSoLogin | |
mqqopensdkapiV2 | |
mqqopensdkapiV3 | |
wtloginmqq2 | |
mqqapi | |
mqqwpa | |
mqzone | |
mqq | |
mqqopensdkapiV4[注:若同时使用QQ和QZONE,则直接添加本格即可] | |
fbauth2 | |
fbauth | |
fbapi20130214 | |
fbapi | |
fbshareextension | |
fbapi20160328 | |
fbapi20150629 | |
twitterauth |
配置ATS
微博平台还需要加上ATS配置:
1.在项目的info.plist中添加 App Transport Security Settings,类型为字典类型
2.给它添加一个Key:Allow Arbitrary Loads,类型为Boolean类型,值为YES;
Twitter 添加V2接口配置
twitter 需要在infoplist加入V2配置,配置MOBTwitterVer
网络强制走https
如果需要强制,在Info.plist配置MOBForceHttps为YES。优先级高于ATS的配置
Facebook 更新配置以及申请权限
请点击 这里
配置Universal Link
新浪微博,微信,QQ,Line特殊配置
新浪微博,微信,QQ,line平台在iOS13上需要校验Universal Link,之前在微信,QQ,新浪微博,line上注册应用需要在配置上Universal Link,另外项目里也要配置上,可以根据我们后台生成的Universal Link去配置:
注意: Team id,Bundle id这些必须要填写的和自己项目里使用的证书的Team id和Bundle id一致,QQ AppID是填写qq初始化的appid,如果需要QQ平台,那么需要填写上,把信息都填写之后保存了才可以使用我们生成的Univesal link
在项目里配置,如下图:
点击Capability,选择Associated Domains,并双击添加,如下图:
填写上Universal Link配置,填写的格式是applinks:xxxx
至此基本的集成流程完成了,可以测试看效果啦!
注意:这个Universal Link也可以自己生成,可以参考这个 苹果官方文档,但是为了方便用户,节省用户的时间和精力,建议直接拷贝我们生成的配置。