授权登录设置
目前支持第三方授权登录的平台有:
如您使用了U-Share的授权登录功能,欢迎尝试使用友盟+智能认证的一键登录功能。一键登录搭配授权登录,登录方式用户可选,一秒认证体验升级,优化注册率和登录率。
授权登录设置
目前支持第三方授权登录的平台有:
国内平台微信、QQ、新浪微博、企业微信、抖音
国外平台Facebook、Twitter、linkedIn、Kakao
用户进行第三方平台授权登录时,平台提供了获取用户资料的授权接口,使用该接口可以获取该三方平台返回的所有资料(包括姓名,头像等)
调用说明
#import <UMShare/UMShare.h>
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:nil completion:^(id result, NSError *error) {
UMSocialUserInfoResponse *resp = result;
// 第三方登录数据(为空表示平台未提供)
// 授权数据
NSLog(@" uid: %@", resp.uid);
NSLog(@" openid: %@", resp.openid);
NSLog(@" accessToken: %@", resp.accessToken);
NSLog(@" refreshToken: %@", resp.refreshToken);
NSLog(@" expiration: %@", resp.expiration);
// 用户数据
NSLog(@" name: %@", resp.name);
NSLog(@" iconurl: %@", resp.iconurl);
NSLog(@" gender: %@", resp.unionGender);
// 第三方平台SDK原始数据
NSLog(@" originalResponse: %@", resp.originalResponse);
}];
}若在4.x及5.x版本中使用微信登录,升级后需参考第三章微信授权登录说明里的【U-Share 4/5升级说明】一节
U-Share封装字段含义
用户授权登录成功后,第三方平台会将用户资料传回,由于各个平台对于用户资料的标识不同,因此为了便于开发者使用,我们将一些常用的字段做了统一封装,开发者可以直接获取,不再需要对不同平台的不同字段名做转换,这里列出我们封装的字段及含义:
UShare封装后字段名 | QQ原始字段名 | 微信原始字段名 | 新浪原始字段名 | 字段含义 | 备注 |
uid | openid | openid | uid | 用户唯一标识 | uid能否实现Android与iOS平台打通,目前QQ只能实现同APPID下用户ID匹配 |
openid | openid | openid | 空 | 用户唯一标识 | 主要为微信和QQ使用 |
unionid | unionid | unionid | uid | 用户唯一标识 | 主要为微信和QQ使用,unionid主要用于微信、QQ用户系统打通 |
usid | openid | openid | uid | 用户唯一标识 | 用于U-Share 4.x/5.x 升级后保留原先使用形式 |
name | screen_name | screen_name | screen_name | 用户昵称 | |
iconurl | profile_image_url | profile_image_url | profile_image_url | 用户头像 |
各平台授权登录详细说明
微信
授权并获取用户信息
获取uid、access token及用户名等注意这里的uid为unionID
// 在需要进行获取用户信息的UIViewController中加入如下代码
#import <UMShare/UMShare.h>
- (void)getAuthWithUserInfoFromWechat
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"Wechat uid: %@", resp.uid);
NSLog(@"Wechat openid: %@", resp.openid);
NSLog(@"Wechat unionid: %@", resp.unionId);
NSLog(@"Wechat accessToken: %@", resp.accessToken);
NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
NSLog(@"Wechat expiration: %@", resp.expiration);
// 用户信息
NSLog(@"Wechat name: %@", resp.name);
NSLog(@"Wechat iconurl: %@", resp.iconurl);
NSLog(@"Wechat gender: %@", resp.unionGender);
// 第三方平台SDK源数据
NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
}
}];
}如果是U-Share4/5版本升级到6.0以上版本,需注意:如果已经使用4/5版本的usid作为服务端用户标识,在新版中保持使用usid字段
unionId简介:开发者在QQ互联平台下拥有的多个移动应用、网站应用,获取的用户unionId是相同的,详情参考微信获取用户基本信息(UnionID机制)。
授权并获取用户信息获取
uid、access token及用户名等
- (void)getAuthWithUserInfoFromQQ
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"QQ uid: %@", resp.uid);
NSLog(@"QQ openid: %@", resp.openid);
NSLog(@"QQ unionid: %@", resp.unionId);
NSLog(@"QQ accessToken: %@", resp.accessToken);
NSLog(@"QQ expiration: %@", resp.expiration);
// 用户信息
NSLog(@"QQ name: %@", resp.name);
NSLog(@"QQ iconurl: %@", resp.iconurl);
NSLog(@"QQ gender: %@", resp.unionGender);
// 第三方平台SDK源数据
NSLog(@"QQ originalResponse: %@", resp.originalResponse);
}
}];
}新浪微博
授权并获取用户信息
获取uid、access token及用户名等
// 在需要进行获取用户信息的UIViewController中加入如下代码
#import <UMShare/UMShare.h>
- (void)getAuthWithUserInfoFromSina
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"Sina uid: %@", resp.uid);
NSLog(@"Sina accessToken: %@", resp.accessToken);
NSLog(@"Sina refreshToken: %@", resp.refreshToken);
NSLog(@"Sina expiration: %@", resp.expiration);
// 用户信息
NSLog(@"Sina name: %@", resp.name);
NSLog(@"Sina iconurl: %@", resp.iconurl);
NSLog(@"Sina gender: %@", resp.unionGender);
// 第三方平台SDK源数据
NSLog(@"Sina originalResponse: %@", resp.originalResponse);
}
}];
}新浪微博(完整版)登录回调方法要求
未安装新浪微博客户端时会弹出Webview进行登录和分享,之后会回调至以下函数:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation如果用户没有重载此函数的话,会导致微博(完整版)SDK通过Webview的回调而崩溃。开发者需重载此方法接受系统回调。
配置ATS以支持访问HTTP请求
由于新浪微博SDK还未更新ATS的支持,故目前需要对其进行配置,可根据所选平台裁剪以下代码:右键info.plist用source code打开
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<!-- 集成新浪微博对应的HTTP白名单-->
<key>sina.com.cn</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.cn</key>
<dict>
<!-- 适配iOS10 -->
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>weibo.cn</key>
<dict>
<!-- 适配iOS10 -->
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>weibo.com</key>
<dict>
<!-- 适配iOS10 -->
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<!-- 新浪微博-->
</dict>
</dict>ADSupport及IDFA的说明
如果开发者希望集成ADSupport.framework,在进行ipa提交store时候勾选用于跟踪用户广告效果的选项即可。如果开发者希望不集成ADSupport.framework,即获取IDFA,可直接删去ADSupport.framework。不集成ADSupport.framework不会影响微博的登录和分享功能。
授权并获取用户信息
(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码
#import <UMShare/UMShare.h>
- (void)getAuthWithUserInfoFromFacebook
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Facebook currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"Facebook uid: %@", resp.uid);
NSLog(@"Facebook accessToken: %@", resp.accessToken);
NSLog(@"Facebook expiration: %@", resp.expiration);
// 用户信息
NSLog(@"Facebook name: %@", resp.name);
// 第三方平台SDK源数据
NSLog(@"Facebook originalResponse: %@", resp.originalResponse);
}
}];
}登录授权流程说明
登录(6.3+版本授权)
Facebook SDK授权流程(在Facebook开发者后台配置):由于新版Facebook SDK的更新,登录流程和之前的流程有很大不同,facebook可以通过配置在facebook官网配置登陆的流程(sso或web),每次启动的时候会获得facebook的serverConfig来决定是否SSO登陆或者web登陆。1. 如果成功获得serverConfig的配置参数,就会根据配置参数来决定选择是否sso或web登陆。2. 如果未成功获得serverConfig的配置参数,就会直接web登陆。
登出(取消授权)(6.3+版本授权)
通过U-Share SDK执行取消Facebook平台授权仅移除APP内登录数据,若希望下次切换其他账号,需要在Safari中登录www.facebook.com,在页面内进行登出操作。
授权并获取用户信息
获取uid、access token及用户名等,注意:Twitter平台仅支持iOS 9.0以上系统
// 在需要进行获取用户信息的UIViewController中加入如下代码
#import <UMShare/UMShare.h>
- (void)getAuthWithUserInfoFromTwitter
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Twitter currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"Twitter uid: %@", resp.uid);
NSLog(@"Twitter accessToken: %@", resp.accessToken);
// 用户信息
NSLog(@"Twitter name: %@", resp.name);
NSLog(@"Twitter iconurl: %@", resp.iconurl);
// 第三方平台SDK源数据
NSLog(@"Twitter originalResponse: %@", resp.originalResponse);
}
}];
}登录授权流程说明(iOS11以下系统)
登录Twitter SDK授权流程:
检测iOS系统内以及安装的Twitter APP有无登录Twitter账号
已在系统及APP登录则直接使用登录账号
未在系统登录则弹出授权页面进行登录,登录成功后同时同步账号到系统Twitter账号
登出(取消授权)通过U-Share SDK执行取消Twitter平台授权仅移除APP内登录数据,若希望下次切换其他账号,需要在系统或Twitter APP内进行登出操作
iOS11及以上系统登录分享操作与微信、微博等平台一致,需要跳转到对应app。
Twitter错误码说明
官方错误码:https://dev.twitter.com/overview/api/response-codes
错误码:401
1、检查在U-Share中设置的Twitter appkey和appsecret是否和Twitter开发者后台相匹配。2、检查Twitter开发者后台中Details选项里的Access level,确保为Read and write权限。
错误码:1871、若在返回错误中包含 “code = 187, duplicated xxx”内容,说明重复发送相同的内容,需过段时间才可发送,或修改发送内容即可。
Linkedin(领英)
授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码
#import <UMShare/UMShare.h>
- (void)getAuthWithUserInfoFromLinkedin
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Linkedin currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"Linkedin uid: %@", resp.uid);
NSLog(@"Linkedin accessToken: %@", resp.accessToken);
NSLog(@"Linkedin expiration: %@", resp.expiration);
// 用户信息
NSLog(@"Linkedin name: %@", resp.name);
// 第三方平台SDK源数据
NSLog(@"Linkedin originalResponse: %@", resp.originalResponse);
}
}];
}Kakao
授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码
#import <UMShare/UMShare.h>
- (void)getAuthWithUserInfoFromKakao
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_KakaoTalk currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授权信息
NSLog(@"Kakao uid: %@", resp.uid);
NSLog(@"Kakao accessToken: %@", resp.accessToken);
NSLog(@"Kakao refreshToken: %@", resp.refreshToken);
// 用户信息
NSLog(@"Kakao name: %@", resp.name);
NSLog(@"Kakao iconurl: %@", resp.iconurl);
// 第三方平台SDK源数据
NSLog(@"Kakao originalResponse: %@", resp.originalResponse);
}
}];
}