美文网首页ios 开发Swift学习笔记Swifter
「Swift学习笔记」使用ShareSDK完成第三方分享功能【M

「Swift学习笔记」使用ShareSDK完成第三方分享功能【M

作者: 元茜姑娘 | 来源:发表于2015-01-07 14:22 被阅读6300次

前段时间用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:

下载.png

2、将下载的SDK解压后导入工程中,勾选”Copy items if needed”:


导入工程.png

3、添加依赖库:


添加依赖库.jpg

SystemConfiguration.framework
QuartzCore.framework
CoreTelephony.framework
libicucore.dylib
libz.1.2.5.dylib
Security.framework

4、新建Header File,并建立桥接:

建立桥接文件.png 链接桥接文件.png

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/快速集成指南/

相关文章

网友评论

  • 小小Xcoder:楼主辛苦了,我问一下微博分享web能带图片吗好像小于32k也显示不了。
  • 76521e381904:你好,问一下,URLscheme不用设置吗
  • Marc_Steven:  然而好像没有什么效果,找不到对应的module也是醉了,已经困扰5分钟了,查阅了很多文档,求解决方案
  • 张嘉夫:这个分享出去,尾巴是哪里呢?是自己的产品吗,还是ShareSDK
  • 6d3fe1f643d5:微营销很火热提供一个做微营销的网址http://wzt.m.cn/
  • 元茜姑娘:@依燃Fly 微信就是对应进去。
  • bfa892275649:@元茜姑娘 求问第五部#import后面的头文件咋写啊?
  • bfa892275649:@依燃Fly 大神,头文件该怎么写?other linker flag怎么弄啊?
  • bfa892275649:@_洪小瑶 大神,头文件该怎么写啊??
  • 055f7a98bcfb:@元茜姑娘 还有,微信的App Key在哪啊??我申请后,只有ID 和 Secret 没有 App Key啊。。。。
  • 055f7a98bcfb:@元茜姑娘 额,我搞定了,错误是 can not found the modual。 原因是我没有写 header 头文件,和 object—C 也未链接,还要添加 other linker flag。
  • 元茜姑娘:@依燃Fly 我那个融合在项目里了,而且工程巨大,不太方便发。你是怎么个不成?报什么错?
  • 055f7a98bcfb:@依燃Fly 我的邮箱 yanlixiang90@gmail.com
  • 055f7a98bcfb:大神,我尝试了好几遍,都不行,有source code 给我参考下吗
  • 俊瑶先森:知道了:#import "ShareSDK/ShareSDK.h"
  • 俊瑶先森:5、打开桥接文件(TEST-Bridging-Header.h)导入文件头:

    #import


    请问导入头文件是导入什么?
    我尝试 #import "ShareSDK.h" 一直失败

本文标题:「Swift学习笔记」使用ShareSDK完成第三方分享功能【M

本文链接:https://www.haomeiwen.com/subject/caztxttx.html