前段時(shí)間用ShareSDK完成了第三方分享的部分喜命,由于說(shuō)明文檔是Objective-C語(yǔ)言,所以將Swift版本整理一下河劝,分享過(guò)來(lái)壁榕。本文以新浪微博(網(wǎng)頁(yè)授權(quán))為例,其他平臺(tái)相似赎瞎。
前期準(zhǔn)備:
1牌里、登陸http://mob.com 并進(jìn)入開(kāi)發(fā)者后臺(tái),添加一個(gè)應(yīng)用务甥,獲得App Key和App Secret牡辽。
2喳篇、登陸新浪微博開(kāi)發(fā)者平臺(tái)http://open.weibo.com ,添加應(yīng)用态辛,獲得App Key和App Secret麸澜。
正式開(kāi)始:
1、進(jìn)入http://sharesdk.mob.com/Download -ShareSDK For iOS專(zhuān)用快速集成組件因妙,下載你所需要的社交平臺(tái)framework:
2痰憎、將下載的SDK解壓后導(dǎo)入工程中,勾選”Copy items if needed”:
3攀涵、添加依賴(lài)庫(kù):
SystemConfiguration.framework
QuartzCore.framework
CoreTelephony.framework
libicucore.dylib
libz.1.2.5.dylib
Security.framework
4铣耘、新建Header File,并建立橋接:
5以故、打開(kāi)橋接文件(TEST-Bridging-Header.h)導(dǎo)入文件頭:
<pre><code>#import <ShareSDK/ShareSDK.h></code></pre>
6蜗细、在Appdelegate.Swift初始化SDK和第三方平臺(tái)(附常見(jiàn)平臺(tái)代碼):
<pre><code>
ShareSDK.registerApp("ShareSDKAppKey")
//新浪微博
ShareSDK.connectSinaWeiboWithAppKey("4071914616", appSecret: "273f52407df87a15cbe06840c64cc0d2", redirectUri: "http://www.weibo.com/balancea")
//豆瓣
ShareSDK.connectDoubanWithAppKey("02e0393e2cfbecb508a0abba86f3c61f", appSecret: "9a000e648fd0cbce", redirectUri: "http://www.ijilu.com")
//QQ空間
ShareSDK.connectQZoneWithAppKey("1103527931", appSecret:"WEKkOPW0NJkc1cwS", qqApiInterfaceCls: QQApiInterface.classForCoder(), tencentOAuthCls: TencentOAuth.classForCoder())
//QQ
ShareSDK.connectQQWithAppId("1103527931", qqApiCls:QQApiInterface.classForCoder())
//鏈接微信
ShareSDK.connectWeChatWithAppId("wx5f09f3b56fd1faf7", wechatCls: WXApi.classForCoder())
//微信好友
ShareSDK.connectWeChatSessionWithAppId("wx5f09f3b56fd1faf7", wechatCls:WXApi.classForCoder())
//微信朋友圈
ShareSDK.connectWeChatTimelineWithAppId("wx5f09f3b56fd1faf7", wechatCls: WXApi.classForCoder())
ShareSDK.connectRenRenWithAppKey("3899f3ffa97544a3a6767ce3d7530142", appSecret: "4a9df27a701742c09d05dbb52ef1483a")</code></pre>
7、在分享按鈕或其他控件的動(dòng)作中添加實(shí)現(xiàn)代碼:
<pre><code>
func share() {
var publishContent : ISSContent = ShareSDK.content("分享文字", defaultContent:"默認(rèn)分享內(nèi)容怒详,沒(méi)內(nèi)容時(shí)顯示",image:nil, title:"提示",url:"返回鏈接",description:"這是一條測(cè)試信息",mediaType:SSPublishContentMediaTypeNews)
ShareSDK.showShareActionSheet(nil, shareList: nil, content: publishContent, statusBarTips: true, authOptions: nil, shareOptions: nil, result: {(type:ShareType,state:SSResponseState,statusInfo:ISSPlatformShareInfo!,error:ICMErrorInfo!,end:Bool) in
println(state.value)
if (state.value == SSResponseStateSuccess.value){
println("分享成功")
var alert = UIAlertView(title: "提示", message:"分享成功", delegate:self, cancelButtonTitle: "ok")
alert.show()
}
else {if (state.value == 2) {
var alert = UIAlertView(title: "提示", message:"您沒(méi)有安裝客戶(hù)端炉媒,無(wú)法使用分享功能!", delegate:self, cancelButtonTitle: "ok")
alert.show()
println(error.errorCode())
println(error.errorDescription())
println()
}
}
})
}</pre></code>
至此昆烁,已可以使用新浪微博網(wǎng)頁(yè)授權(quán)進(jìn)行分享吊骤,如需使用客戶(hù)端,只需添加SSO授權(quán)即可静尼。
另外白粉,新注冊(cè)騰訊開(kāi)放平臺(tái)帳號(hào)只支持SSO授權(quán),只是老開(kāi)發(fā)者才可以使用網(wǎng)頁(yè)授權(quán)鼠渺。
豆瓣開(kāi)發(fā)者平臺(tái)新建應(yīng)用后鸭巴,需要添加測(cè)試ID,并使用此ID進(jìn)行分享測(cè)試拦盹。
原開(kāi)發(fā)文檔:http://wiki.mob.com/快速集成指南/