美文网首页友盟
友盟实现第三方登录

友盟实现第三方登录

作者: LH_0811 | 来源:发表于2016-08-01 10:31 被阅读2129次

第一步 下载sdk

http://dev.umeng.com/social/ios/sdk-download?spm=0.0.0.0.nGfVZk

第二步 修改info.plist文件 适配ios9

1.  HTTP传输安全
以iOS9 SDK编译的工程会默认以SSL安全协议进行网络传输,即HTTPS,如果依然使用HTTP协议请求网络会报系统异常并中断请求。目前可用如下两种方式保持用HTTP进行网络连接:

A、在info.plist中加入安全域名白名单(右键info.plist用source code打开)

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>log.umsns.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
        <key>sns.whalecloud.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>

        <!-- 集成新浪微博对应的HTTP白名单-->
        <key>sina.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>weibo.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>weibo.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>sinaimg.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>sinajs.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>sina.com.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <!-- 新浪微博-->

        <!-- 集成微信、QQ、Qzone、腾讯微博授权对应的HTTP白名单-->
        <key>qq.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <!-- 腾讯授权-->

        <!-- 集成人人授权对应的HTTP白名单-->
        <key>renren.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <!-- 人人授权-->

        <!-- 集成Facebook授权对应的HTTP白名单-->
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>                
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <!-- Facebook授权-->

        <!-- 集成Twitter授权对应的HTTP白名单-->
        <key>twitter.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>                
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <!-- Twitter授权-->
    </dict>
</dict>

注:以上部分平台官方未给出相应白名单,由技术人员测试各个平台所收集而来,如果有所遗漏,请自行加入并向客服说明,我们会进一步补充名单。

B、在info.plist的NSAppTransportSecurity下新增NSAllowsArbitraryLoads并设置为YES,指定所有HTTP连接都可正常请求 http://dev.umeng.com/social/ios/ios9

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

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

<key>LSApplicationQueriesSchemes</key>
<array>
    <!-- 微信 URL Scheme 白名单-->
    <string>wechat</string>
    <string>weixin</string>

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

    <!-- QQ、Qzone URL Scheme 白名单-->
    <string>mqqapi</string>
    <string>mqq</string>
    <string>mqqOpensdkSSoLogin</string>
    <string>mqqconnect</string>
    <string>mqqopensdkdataline</string>
    <string>mqqopensdkgrouptribeshare</string>
    <string>mqqopensdkfriend</string>
    <string>mqqopensdkapi</string>
    <string>mqqopensdkapiV2</string>
    <string>mqqopensdkapiV3</string>
    <string>mqzoneopensdk</string>
    <string>wtloginmqq</string>
    <string>wtloginmqq2</string>
    <string>mqqwpa</string>
    <string>mqzone</string>
    <string>mqzonev2</string>
    <string>mqzoneshare</string>
    <string>wtloginqzone</string>
    <string>mqzonewx</string>
    <string>mqzoneopensdkapiV2</string>
    <string>mqzoneopensdkapi19</string>
    <string>mqzoneopensdkapi</string>
    <string>mqqbrowser</string>
    <string>mttbrowser</string>

    <!-- 支付宝 URL Scheme 白名单-->
    <string>alipay</string>
    <string>alipayshare</string>


    <!-- 人人 URL Scheme 白名单-->
    <string>renrenios</string>
    <string>renrenapi</string>
    <string>renren</string>
    <string>renreniphone</string>

    <!-- 来往 URL Scheme 白名单-->
    <string>laiwangsso</string>

    <!-- 易信 URL Scheme 白名单-->
    <string>yixin</string>
    <string>yixinopenapi</string>

    <!-- instagram URL Scheme 白名单-->
    <string>instagram</string>

    <!-- whatsapp URL Scheme 白名单-->
    <string>whatsapp</string>

    <!-- line URL Scheme 白名单-->
    <string>line</string>

    <!-- Facebook URL Scheme 白名单-->
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

注:以上部分平台官方未给出相应白名单,由技术人员测试各个平台所收集而来,如果有所遗漏,请自行加入并向客服说明,我们会进一步补充名单。

第三步 集成UMSDK

解压SDK压缩包,将形如UMSocial_Sdk_x.x.x,UMSocial_Sdk_Extra_Frameworks的文件夹拖入工程目录。编译。如果报错就添加需要的framwork。

添加系统需要的framework
添加SDK后需要手动添加系统库SystemConfiguration.framework

在other linker flags增加-ObjC 选项,并添加ImageIO 系统framework(实现新浪微博必须完成的步骤)

添加SDK依赖的系统库文件

在Xcode中打开工程配置文件,选择“summary”一栏。在“summary”中选择“Linked Frameworks and Libraries”一栏,点击“+”图标添加下面八个库文件,分别是

Security.framework
libiconv.dylib
SystemConfiguration.framework
CoreGraphics.Framework
libsqlite3.dylib
CoreTelephony.framework
libstdc++.dylib
libz.dylib
1.3  设置友盟appkey
获取友盟Appkey。如果你之前已经在友盟注册了应用,获得了Appkey,可以继续使用之前获得Appkey。

如果你尚未在友盟注册账号,需要先注册,注册之后登录你的账号,点击添加新应用,完成新应用填写之后,将进入应用管理页面。在该页面就能得到Appkey。

在代码中设置你的友盟Appkey,在 AppDelegate文件内设置你的AppKey:

#import "UMSocial.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
}

配置第三方APPID


这里只列出最常用的微信、QQ及新浪微博的配置方法,其他平台如Facebook等请参考对应文档配置, 申请平台APPID请直接参考文档申请第三方账号

在APPdelegate.m文件中增加对应下面的配置

#import "UMSocial.h"     
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //设置友盟社会化组件appkey
    [UMSocialData setAppKey:UmengAppkey];
    //设置微信AppId、appSecret,分享url
    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
    //设置手机QQ 的AppId,Appkey,和分享URL,需要#import "UMSocialQQHandler.h"
    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
    //打开新浪微博的SSO开关,设置新浪微博回调地址,这里必须要和你在新浪微博后台设置的回调地址一致。需要 #import "UMSocialSinaSSOHandler.h"
   [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"3921700954"
                                              secret:@"04b48b094faeb16683c32669824ebdad"
                                         RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
}

第四步 平台 url scheme设置格式

新浪微博    “wb”+新浪appkey,例如“wb126663232”
微信  微信应用appId,例如“wxd9a39c7122aa6516”,微信详细集成步骤参考微信集成方法
QQ、QQ空间 需要添加两个URL schemes 1. “QQ”+腾讯QQ互联应用appId转换成十六进制(不足8位前面补0),例如“QQ05FC5B14”,注意大写,生成十六进制方法:点击链接,2.“tencent“+腾讯QQ互联应用Id,例如“tencent100424468" ,QQ及Qzone详细集成步骤参考手机QQ集成方法
Facebook    集成最新Facebook SDK在iOS7.0以上有效,若要使用我们提供的facebook分享需要设置“fb”+facebook AppID,例如“fb1440390216179601”,详细集成方法见集成facebook

屏幕快照 2016-08-01 上午10.29.59.png

第五步 登录调用

1.  第三方登录概述
第三方登录主要用于简化用户登录流程,通过用户拥有的微博、QQ、微信等第三方账号进行登录并且构建APP自己的登录账号体系。

实现第三方登录主要通过下面两步:

在第三方平台完成授权
获取第三方平台用户资料
注意:

第三方登录所需要的相关库文件、第三方APPID及相关配置都与分享完全一致,请直接参考分享对应文档配置
开发者无需关心access_token的有效性,使用我们的登录接口可以直接返回对应的用户资料,登录体系中只需要将这些用户资料(包括用户id)管理起来即可

2.  第三方登录支持的平台
目前友盟社会化组件支持的第三方登录平台为:新浪微博、微信、QQ、QQ空间、腾讯微博、人人网、豆瓣、Facebook、Twitter


2.1  新浪微博登录
在AppDelegate中添加回调方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK,例如支付宝SDK等
    }
    return result;
}
在新浪微博登录按钮中实现下面的方法

UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];

        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){

//          获取微博用户名、uid、token等

            if (response.responseCode == UMSResponseCodeSuccess) {

                 NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);

            }});

所有的平台用户信息都在thirdPlatformUserProfile中返回
删除授权调用下面的方法

[[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"response is %@",response);
    }];

2.2  QQ及Qzone登录
QQ登录只支持SSO登录方式,必须具备手机QQ客户端,Qzone默认调用SSO登录

添加配置文件参考文档:添加QQ及Qzone,添加相关库文件,配置URL schemes及添加系统回调

在AppDelegate应用入口方法添加下面的方法

    //设置分享到QQ/Qzone的应用Id,和分享url 链接
    #import "UMSocialQQHandler.h"
    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];

在QQ登录按钮中实现下面的方法

UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];

        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){

//          获取微博用户名、uid、token等

            if (response.responseCode == UMSResponseCodeSuccess) {

                 NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);

            }});

所有的平台用户信息都在thirdPlatformUserProfile中返回
Qzone登录将UMShareToQQ替换为UMShareToQzone即可


2.3  微信登录
添加配置文件参考文档:添加微信及朋友圈,添加相关库文件,配置URL schemes及添加系统回调

注意微信登录必须先在微信开放平台申请微信登录权限
在你的程序APPdelegate入口方法添加下面的代码

  #import "UMSocialWechatHandler.h"
    //设置微信AppId、appSecret,分享url
    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];

在微信登录按钮中实现下面的方法

  UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];

        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){

            if (response.responseCode == UMSResponseCodeSuccess) {

                NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);

            }

 });

所有的平台用户信息都在thirdPlatformUserProfile中返回

删掉授权信息

[[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"response is %@",response);
    }];

相关文章

网友评论

    本文标题:友盟实现第三方登录

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