swift利用友盟实现新浪微博SSO授权登录

swift利用友盟实现新浪微博SSO授权登录

字数1103  阅读821  评论1 

1. 获取友盟Appkey

  • 如果你尚未在友盟注册开发者账号,需要先注册,注册之后登录你的账号,点击添加新应用,填写完应用基本信息后,将进入"下载SDK并添加代码"页面,此页面即可得到Appkey。
  • 如果你之前已经在友盟注册了应用,可以继续使用它的Appkey。 

2. 下载并安装SDK

2.1 下载SDK

最新版SDK下载地址:http://dev.umeng.com/social/ios/sdk-download


2.2 加入SDK

UMSocial_Sdk_x.x.x的文件夹以及UMSocial_Sdk_Extra_Frameworks目录下的SinaSSO文件夹拖入工程目录:

2.3 创建桥接

具体如何桥接这里就不说明了,我们需要在桥接文件中包含以下两个头文件

#import "UMSocial.h"
#import "UMSocialSinaSSOHandler.h"
2.4 设置AppKey

在AppDelegate内设置友盟AppKey

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // 设置Appkey
    UMSocialData.setAppKey("5608de9de0f55afaae0018e6")
    return true
}
2.5 添加实现代码

在新浪微博登录按钮中实现下面的方法

let snsPlatform: UMSocialSnsPlatform = UMSocialSnsPlatformManager.getSocialPlatformWithName(UMShareToSina)

snsPlatform.loginClickHandler(self, UMSocialControllerService.defaultControllerService(), true, {response in

    if response.responseCode == UMSResponseCodeSuccess {

        let snsAccount:UMSocialAccountEntity = UMSocialAccountManager.socialAccountDictionary()[UMShareToSina] as! UMSocialAccountEntity

        print("username is \(snsAccount.userName), uid is \(snsAccount.usid), token is \(snsAccount.accessToken) url is \(snsAccount.iconURL)")
    }
})

在授权完成后调用获取用户信息的方法

//获取accestoken以及新浪用户信息,得到的数据在回调Block对象形参respone的data属性
UMSocialDataService.defaultDataService().requestSnsInformation(UMShareToSina) { (response) -> Void in
    print(response.data)
}

3. SSO配置

使用SSO授权方式,在用户安装了微博客户端并登录时,可以在分享过程中不需要输入账号密码,直接通过微博客户端授权,随后进行网页分享,免去了用户输入密码的过程。 在用户未安装客户端时,则自动跳转到网页授权方式,微博SSO授权友盟提供了微博原生SDK与非原生SDK两种方式,本文介绍的是微博原生SDK。

3.1 添加相关系统库文件

在Xcode中打开工程配置文件,选择“Linked Frameworks and Libraries”一栏,点击“+”图标添加下列库文件:
在other linker flags增加-ObjC 选项,并添加ImageIO.framework
Security.framework
libiconv.dylib
SystemConfiguration.framework
CoreGraphics.Framework
libsqlite3.dylib
CoreTelephony.framework
libstdc++.dylib
libz.dylib

3.2 添加URL schemes

在你的工程设置项,targets 一栏下,选中自己的 target,在 Info->URL Types 中添加 URL Schemes,格式为“wb”+新浪appkey,例如“wb126663232”

3.3 在AppDelegate文件集成相应的开关方法:
  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
   //打开新浪微博的SSO开关,设置新浪微博回调地址,这里必须要和你在新浪微博后台设置的回调地址一致。若在新浪后台设置我们的回调地址,“http://sns.whalecloud.com/sina2/callback”,这里可以传nil
    UMSocialSinaSSOHandler.openNewSinaSSOWithAppKey("2923156246", redirectURL: "http://sns.whalecloud.com/sina2/callback")

    return true
}
3.4 在AppDelegate文件里面实现下面的系统回调方法
    func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
        return UMSocialSnsService.handleOpenURL(url)
    }
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        return UMSocialSnsService.handleOpenURL(url)
    }}

4. 适配iOS9系统

  • iOS9网络传输适配
    在info.plist的NSAppTransportSecurity下新增NSAllowsArbitraryLoads并设置为YES,指定所有HTTP连接都可正常请求

    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
  • 应用跳转白名单
    如果你的应用使用了如SSO授权登录或跳转分享功能,在iOS9下就需要增加一个可跳转的白名单,指定对应跳转App的URL Scheme,否则将在第三方平台判断是否跳转时用到的canOpenURL时返回NO,进而只进行webview授权或授权/分享失败。
    同样在info.plist的LSApplicationQueriesSchemes下增加:

    <key>LSApplicationQueriesSchemes</key>
    <array>
    <!-- 新浪微博 URL Scheme 白名单-->
      <string>sinaweibohd</string>
      <string>sinaweibo</string>
      <string>sinaweibosso</string>
      <string>weibosdk</string>
      <string>weibosdk2.5</string>
    </array>

提示,如果程序报以下错误:

ld: ‘/Users/**/Framework/SDKs/PolymerPay/Library/mobStat/lib**SDK.a(**ForSDK.o)’ does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64

可以在Build Settings中将Enable Bitcode设置为No

最后附上示例Demo的github地址:https://github.com/laichunhui/UMSocialLogin_Sina

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值