前段時間用ShareSDK完成了第三方分享的部分,由于說明文檔是Objective-C語言,所以將Swift版本整理一下,分享過來。本文以新浪微博(網頁授權)為例,其他平臺相似。
前期準備:
1、登陸http://mob.com 并進入開發者后臺,添加一個應用,獲得App Key和App Secret。
2、登陸新浪微博開發者平臺http://open.weibo.com ,添加應用,獲得App Key和App Secret。
正式開始:
1、進入http://sharesdk.mob.com/Download -ShareSDK For iOS專用快速集成組件,下載你所需要的社交平臺framework:
2、將下載的SDK解壓后導入工程中,勾選”Copy items if needed”:
3、添加依賴庫:
SystemConfiguration.framework
QuartzCore.framework
CoreTelephony.framework
libicucore.dylib
libz.1.2.5.dylib
Security.framework
4、新建Header File,并建立橋接:
5、打開橋接文件(TEST-Bridging-Header.h)導入文件頭:
<pre><code>#import <ShareSDK/ShareSDK.h></code></pre>
6、在Appdelegate.Swift初始化SDK和第三方平臺(附常見平臺代碼):
<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、在分享按鈕或其他控件的動作中添加實現代碼:
<pre><code>
func share() {
var publishContent : ISSContent = ShareSDK.content("分享文字", defaultContent:"默認分享內容,沒內容時顯示",image:nil, title:"提示",url:"返回鏈接",description:"這是一條測試信息",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:"您沒有安裝客戶端,無法使用分享功能!", delegate:self, cancelButtonTitle: "ok")
alert.show()
println(error.errorCode())
println(error.errorDescription())
println()
}
}
})
}</pre></code>
至此,已可以使用新浪微博網頁授權進行分享,如需使用客戶端,只需添加SSO授權即可。
另外,新注冊騰訊開放平臺帳號只支持SSO授權,只是老開發者才可以使用網頁授權。
豆瓣開發者平臺新建應用后,需要添加測試ID,并使用此ID進行分享測試。
原開發文檔:http://wiki.mob.com/快速集成指南/