美文网首页友盟
iOS开发 swift -- Umeng第三方登录

iOS开发 swift -- Umeng第三方登录

作者: Hayley__ | 来源:发表于2017-04-07 17:41 被阅读826次

一 注册友盟账号

登录友盟官网,添加新的应用,获取Appkey

相关链接

二 申请第三方账号

相关链接

三 sdk集成 CocoaPods

$ cd/你的项目地址
$ open -e Podfile

target '你的app' do
   # U-Share SDK UI模块(分享面板,建议添加)
   pod ‘UMengUShare/UI’

   # 集成微信(精简版0.2M)
   pod ‘UMengUShare/Social/ReducedWeChat'

   # 集成微信(完整版14.4M)
   pod ‘UMengUShare/Social/WeChat'

   # 集成QQ(精简版0.5M)
   pod ‘UMengUShare/Social/ReducedQQ'

   # 集成QQ(完整版7.6M)
   pod ‘UMengUShare/Social/QQ'

   # 集成新浪微博(精简版1M)
   pod ‘UMengUShare/Social/ReducedSina'

   # 集成新浪微博(完整版25.3M)
   pod ‘UMengUShare/Social/Sina'
end

$ pod install

四 配置文件

1.配置HTTP网络连接
在info.plist中加入安全域名白名单(右键info.plist用source code打开)
<key>NSAppTransportSecurity</key>  
     <dict>  
           <key>NSAllowsArbitraryLoads</key>  
       <true/>  
     </dict>  
2.配置可跳转的白名单
<key>LSApplicationQueriesSchemes</key>  
    <array>  
        <string>sinaweibo</string>  
        <string>sinaweibohd</string>  
        <string>weibosdk2.5</string>  
        <string>weibosdk</string>  
        <string>sinaweibosso</string>  
        <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>  
3.配置url scheme 用于跳出应用之后可以返回

targets -> Info -> URL Types

平台 格式 举例 备注
微信 微信appKey wxdc1e388c3822c80b
QQ/Qzone 需要添加两项URL Scheme:1、"tencent"+腾讯QQ互联应用appID2、“QQ”+腾讯QQ互联应用appID转换成十六进制(不足8位前面补0) 如appID:100424468 1、tencent100424468 2、QQ05fc5b14 QQ05fc5b14为100424468转十六进制而来,因不足8位向前补0,然后加"QQ"前缀
新浪微博 “wb”+新浪appKey wb3921700954

点击查看更多详情

4.为app瘦身(苹果官方会处理资源包,用户下载资源包变小)

targets -> Build Settings -> Enable Bitcode -> No

五 代码示例

    //设置Umeng Appkey 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // 友盟
        UMSocialManager.default().umSocialAppkey ="appkey"
        
        UMSocialManager.default().setPlaform(.wechatSession, appKey: "key", appSecret: "secret", redirectURL: url)
        
        UMSocialManager.default().setPlaform(.QQ, appKey: "key", appSecret: "secret", redirectURL: url)
        
        UMSocialManager.default().setPlaform(.sina, appKey: "key", appSecret: "secret", redirectURL: "必须要和你在新浪微博后台设置的回调地址一致")
    }

    //并添加系统回调方法
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let result = UMSocialManager.default().handleOpen(url)
        if !result {
                //返回其他
        }
        return result
    }

六 登录点击事件

    func loginClick() {
        UMSocialDataManager.default().clearAllAuthorUserInfo()
        UMSocialManager.default().getUserInfo(with: .QQ, currentViewController: self, completion: { (result, userError) in
            if (result != nil) {
                //获取用户的个人信息
                let userInfo = result as! UMSocialUserInfoResponse
                    userInfo.uid...
               //调用本地登录接口
            }else
            {
                debugPrint("--",userError.debugDescription)
            }
        })
    }

如有不妥 请多多指教:)

相关文章

网友评论

    本文标题:iOS开发 swift -- Umeng第三方登录

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