Umeng第三方登录(Swift)

为了集成第三方登录,自己捣鼓了一上午,顺便记下来,以后用起来更方便(以qq、微信、微博为例)
1、集成cocoapod,简单方便

# 集成微信(精简版0.2M)
pod ‘UMengUShare/Social/ReducedWeChat'
# 集成QQ(精简版0.5M)
pod ‘UMengUShare/Social/ReducedQQ'
# 集成新浪微博(精简版1M)
pod ‘UMengUShare/Social/ReducedSina'

2、使用桥接文件导入

//友盟登录
#import 

3、delegate里面的集成

private func Umeng() {
        
        /* 打开调试日志 */
        UMSocialManager.default().openLog(true)
        // 打开图片水印
        UMSocialGlobal.shareInstance().isClearCacheWhenGetUserInfo = false
        /* 设置友盟appkey */
        UMSocialManager.default().umSocialAppkey = "5861e5daf5ade41326001eab"
        
        configUSharePlatforms()
    }

    func configUSharePlatforms() {
        
        /* 设置微信的appKey和appSecret */
        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: "wxdc1e388c3822c80b", appSecret: "3baf1193c85774b3fd9d18447d76cab0", redirectURL: "http://mobile.umeng.com/social")
        /*
         * 移除相应平台的分享,如微信收藏
         */
        //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
        
        /* 设置分享到QQ互联的appID
         * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
         100424468.no permission of union id
         */
        UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: "1105821097", appSecret: nil, redirectURL: "http://mobile.umeng.com/social")
        
        /* 设置新浪的appKey和appSecret */
        UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: "829445432", appSecret: "0adf7bacb3f5d86bb790706010790bb8", redirectURL: "http://mobile.umeng.com/social")

    }
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        
        let result = UMSocialManager.default().handleOpen(url, sourceApplication: sourceApplication, annotation: annotation)
        if !result {
            //其他如支付等SDK的回调
        }
        return result
    }

注意:每一个都需要去相应的开放平台去注册自己的应用,不同的开放平台需要设置不同的信息
4、设置URL Types

C303933E-59E3-47E1-8D97-9BE829320C0E.png

配置第三方的URL Scheme

84D6076A-58C9-4DF3-9651-940CFC510174.png

5、配置SSO白名单(在info.plist的源文件里面添加)

LSApplicationQueriesSchemes

    
    wechat
    weixin

    
    sinaweibohd
    sinaweibo
    sinaweibosso
    weibosdk
    weibosdk2.5

    
    mqqapi
    mqq
    mqqOpensdkSSoLogin
    mqqconnect
    mqqopensdkdataline
    mqqopensdkgrouptribeshare
    mqqopensdkfriend
    mqqopensdkapi
    mqqopensdkapiV2
    mqqopensdkapiV3
    mqqopensdkapiV4
    mqzoneopensdk
    wtloginmqq
    wtloginmqq2
    mqqwpa
    mqzone
    mqzonev2
    mqzoneshare
    wtloginqzone
    mqzonewx
    mqzoneopensdkapiV2
    mqzoneopensdkapi19
    mqzoneopensdkapi
    mqqbrowser
    mttbrowser

6、添加授权,和回调函数(可以获取到你所需要的信息)

func getAuthWithUserInfoFromSina(platformType: UMSocialPlatformType) {
        
        UMSocialManager.default().getUserInfo(with: platformType, currentViewController: nil) { (result: Any?, error: Error?) in
            if (error != nil) { //打印错误信息
                print("%@", error!)
            } else {
                let resp = result as! UMSocialUserInfoResponse
                
                //授权信息
                print("sina uid: %@",resp.uid)
                print("sina accesstoken: %@",resp.accessToken)
                print("sina refreshtoken: %@",resp.refreshToken)
                print("sina expriration: %@", resp.expiration)
                //用户信息
                print("sina name: %@", resp.name)
                print("sina iconurl: %@",resp.iconurl)
                print("sina gender: %@", resp.unionGender)
                //第三方平台SDK源数据
                print("sina originalResponse: %@", resp.originalResponse)
            }
        }
    }

!!!注意:
1)如果出现One of the two will be used. Which one is undefined.这个错误。

/* 设置新浪的appKey和appSecret */
        UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: "11111111111", appSecret: "0adf7bacb3f5d86bb790706010790bb8", redirectURL: "http://www.baidu.com")

redirectURL需要从你创建的开放平台里面的授权回调URL和取消授权回调URL选取一个就好了

你可能感兴趣的:(Umeng第三方登录(Swift))