关于第三方分享和登录本身就是一个挺大的坑, Swift的第三方分享和登录就更不用说了, 坑上加坑, 这里记录下来是如何把ShareSDK给集成到项目中去的
也好久没有更新Blog了, 今天终于有时间来填上这个坑了, 顺便一提, 过完年这段时间都没怎么写iOS了, 也就是维护维护以前的代码, 也不知道这次能不能, 完成这个demo的继承, 哎.. 无所谓了, 不成就删了这篇文章了, 就这么愉快的决定了.
还有一点需要说明下, 我使用的是shareSDK2.x版本不是最新版, 原因是最新版, 我按照官方的集成方法集成失败了, 现在也没能找到什么原因, 不过也就这样了, 反正2.x能用对吧.
准备
这里就不墨迹的去说明如何去下载安装包了, 看下面的参考资料就好了, 里面有相关说明, 我这里就不再赘述了. 见谅
下载到的文件如下图:
配置
把下载到的ShareSDK包拖动到项目目录中
添加相关依赖库
1 2 3 4 5 6 7 8 9 10 11 12
| JavaScriptCore.framework ImageIO.framework CoreLocation.framework MessageUI.framework Social.framework Accounts.framework
libicucore.tbd libz.tbd libstdc++.tbd libsqlite3.dylib
|
Ps: 命名规则 projectNmae-Bridging-Header.h // 并不是必须得这样写, 只是官方推荐这样写
设置桥接
Ps: 里面填写的路径是相对于你项目根路径bridging-header文件的路径 // 是实际的在finder中的路径而不是在Xcode中所看到的路径
把需要桥接的累写入bridging-header文件内, 这样就可以通过Swift进行访问了
1 2 3 4 5 6 7 8 9 10 11 12 13
| #import <ShareSDK/ShareSDK.h>
#import "WXApi.h" #import "WXApiObject.h"
#import "WeiboSDK.h"
#import <TencentOpenAPI/QQApiInterface.h> #import <TencentOpenAPI/TencentOAuth.h>
|
配置AppDelegate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { ShareSDK.registerApp("807e39c0a100") ShareSDK.connectWeChatWithAppId("wx7259198a010a442b", appSecret: "d4624c36b6795d1d99dcf0547af5443d", wechatCls: WXApi.classForCoder()) ShareSDK.connectWeChatSessionWithAppId("wx7259198a010a442b", wechatCls:WXApi.classForCoder()) ShareSDK.connectWeChatTimelineWithAppId("wx7259198a010a442b", wechatCls: WXApi.classForCoder()) ShareSDK.connectSinaWeiboWithAppKey("2883129486", appSecret: "ae926077609479eb7b46417dd0adfb06", redirectUri: "http://www.sina.com") ShareSDK.connectQQWithAppId("101165144", qqApiCls:QQApiInterface.classForCoder()) ShareSDK.connectQZoneWithAppKey("101165144", appSecret: "b8e4e1e7af7bb07e647e036c34b868bd", qqApiInterfaceCls: QQApiInterface.classForCoder(), tencentOAuthCls: TencentOAuth.classForCoder()) ShareSDK.connectQQWithQZoneAppKey("101165144", qqApiInterfaceCls: QQApiInterface.classForCoder(), tencentOAuthCls: TencentOAuth.classForCoder()) ShareSDK.connectTencentWeiboWithAppKey("101165144", appSecret: "b8e4e1e7af7bb07e647e036c34b868bd", redirectUri: "http://www.sharesdk.cn") ShareSDK.connectSMS() ShareSDK.connectMail() return true }
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool { return ShareSDK.handleOpenURL(url, wxDelegate: self) }
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { return ShareSDK.handleOpenURL(url, sourceApplication: sourceApplication, annotation: annotation, wxDelegate: self) }
|
配置info.plist
添加URLSchemes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>wx7259198a010a442b</string> </array> </dict> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>QQ41C4787D</string> </array> </dict> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>tencent101165144</string> </array> </dict> </array>
|
添加白名单
原因: http://wiki.mob.com/ios9-对sharesdk的影响(适配ios-9必读)/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <key>LSApplicationQueriesSchemes</key> <array> <string>mqqOpensdkSSoLogin</string> <string>mqzone</string> <string>sinaweibo</string> <string>alipayauth</string> <string>alipay</string> <string>safepay</string> <string>mqq</string> <string>mqqapi</string> <string>mqqopensdkapiV3</string> <string>mqqopensdkapiV2</string> <string>mqqapiwallet</string> <string>mqqwpa</string> <string>mqqbrowser</string> <string>wtloginmqq2</string> <string>weixin</string> <string>wechat</string> </array>
|
使用ShareSDK
我平常用到的有三块, 这里我一一列举出来, 其它没用到过的功能, 就不会出现在下面了, 可能以后用到了, 会回来更新这篇文章的
判断是否安装客户端
1 2 3 4 5 6 7
| if !WXApi.isWXAppInstalled() { print("没有安装微信~") }
if !QQApi.isQQInstalled() { print("没有安装QQ~") }
|
分享
不那么多废话了, 直接上代码, 注释有说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| let url = "http://huyaohui.com/Niconiconi/"
let image: ISSCAttachment? = nil let title: String = "来自SwiftShareSDK分享" let description: String = "あの日見た花の名前を僕達はまだ知らない。 http://huyaohui.com/Niconiconi/" let defaultContent: String = "这里是默认输入的内容"
let publishContent = ShareSDK.content(title, defaultContent: defaultContent, image: image, title: title, url: url, description: description, mediaType: SSPublishContentMediaTypeNews)
ShareSDK.showShareActionSheet(nil, shareList: nil, content: publishContent, statusBarTips: true, authOptions: nil, shareOptions: nil) { (type: ShareType, state: SSResponseState, statusInfo: ISSPlatformShareInfo!, error: ICMErrorInfo!, end: Bool) -> Void in if state.rawValue == SSResponseStateBegan.rawValue { print("开始分享~") } else if state.rawValue == SSResponseStateSuccess.rawValue { print("success~") } else if state.rawValue == SSResponseStateFail.rawValue { print("分享失败~") print(error) } else if state.rawValue == SSResponseStateCancel.rawValue { print("用户点击取消分享~") } if type.rawValue == ShareTypeSinaWeibo.rawValue { print("新浪微博分享") } else if type.rawValue == ShareTypeTencentWeibo.rawValue { print("腾讯微博分享") } else if type.rawValue == ShareTypeQQ.rawValue { print("QQ分享") } else if type.rawValue == ShareTypeQQSpace.rawValue { print("QQ空间分享") } else if type.rawValue == ShareTypeWeixiSession.rawValue { print("微信好友分享") } else if type.rawValue == ShareTypeWeixiTimeline.rawValue { print("微信朋友圈分享") } }
|
第三方登录
其实第三方登录就只是拿到一个回调的uid而已, 然后保存这个uid作为表示就行了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
let shareType: ShareType = ShareTypeWeixiTimeline
ShareSDK.getUserInfoWithType(shareType, authOptions: nil) { (result, userinfo: ISSPlatformUser!, error: ICMErrorInfo!) -> Void in if result { print(userinfo.uid()!) } else { if error.errorCode() == -6004 { print("您没有安装QQ") } else if error.errorCode() == -22003 { print("您没有安装微信") } else { print("第三方登录失败") } } }
|
项目地址
完整demo: https://github.com/MakeHui/SwiftShareSDK
总结
虽然被坑了无数次, 才彻底把自己需要的功能摸清了, 哎… 不容易啊.
对了这篇文章是在我真正搞懂ShareSDK之后很久才写的, 算是一个复习吧, 已经很久没写Swift了, 将来也可能会很长一段时间不写再写Swift了, 悲剧…
参考资料
http://www.jianshu.com/p/e374732f54f0