1. 三方登录的概念

1.1  第三方登录的定义

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

1.2  如何实现三方登录

三方登录所添加的SDK和分享功能相同,在这里不再复述,具体工程配置、系统回调及URL scheme配置参考对应分享文档即可 分享文档

最终通过调用登录接口,获取用户在第三方平台的用户ID、头像等资料完成账号体系的构建

1.3  三方登录支持的平台

  • 国内平台

微信、QQ、新浪、腾讯微博、人人网、豆瓣

  • 国外平台

Facebook、Twitter、linkedIn、Kakao

2.  代码集成

// 在需要进行获取登录信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.h>- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType{  

[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self 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版本中使用微信登录,升级后需参考说明:4.x/5.x版本升级(授权信息变化)

登录成功后,第三方平台会将用户资料传回,由于各个平台对于用户资料的标识不同,因此为了便于开发者使用,我们将一些常用的字段做了统一封装,开发者可以直接获取,不再需要对不同平台的不同字段名做转换,这里列出我们封装的字段及含义

UShare封装后字段名 QQ原始字段名 微信原始字段名 新浪原始字段名 字段含义 备注
uid openid unionid 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 用户昵称  
gender gender gender gender 用户性别 该字段会直接返回男女
iconurl profile_image_url profile_image_url profile_image_url 用户头像  

3.  不同平台的登录调用方法

3.1  新浪微博:

  • 授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.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.u    id);            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);        }    }];}

3.2  qq:

  • 授权并获取用户信息(获取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);        }    }];}

3.3  微信:

  • 授权并获取用户信息(获取uid、access token及用户名等)

注意这里的uid为unionID

// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.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);        }    }];}

3.4  Linkedin(领英)

  • 授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.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);        }    }];}

3.5  Facebook

  • 授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.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);        }    }];}

3.6  Twitter

  • 授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.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);        }    }];}

3.7  腾讯微博

  • 授权并获取用户信息(获取uid、access token等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.h>- (void)getAuthWithUserInfoFromTencentWeibo{    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_TencentWb currentViewController:nil completion:^(id result, NSError *error) {        if (error) {        } else {            UMSocialUserInfoResponse *resp = result;            // 授权信息            NSLog(@"TencentWeibo uid: %@", resp.uid);            NSLog(@"TencentWeibo accessToken: %@", resp.accessToken);            NSLog(@"TencentWeibo expiration: %@", resp.expiration);            // 第三方平台SDK源数据            NSLog(@"TencentWeibo originalResponse: %@", resp.originalResponse);        }    }];}

3.8  豆瓣

  • 授权并获取用户信息(获取uid、access token等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.h>- (void)getAuthWithUserInfoFromDouban{    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Douban currentViewController:nil completion:^(id result, NSError *error) {        if (error) {        } else {            UMSocialUserInfoResponse *resp = result;            // 授权信息           NSLog(@"Douban uid: %@", resp.uid);            NSLog(@"Douban accessToken: %@", resp.accessToken);            NSLog(@"Douban expiration: %@", resp.expiration);            // 第三方平台SDK源数据            NSLog(@"Douban originalResponse: %@", resp.originalResponse);       }    }];}

3.9  人人

  • 授权并获取用户信息(获取uid、access token等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.h>- (void)getAuthWithUserInfoFromRenren{    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Renren currentViewController:nil completion:^(id result, NSError *error) {        if (error) {        } else {            UMSocialUserInfoResponse *resp = result;            // 授权信息            NSLog(@"Renren uid: %@", resp.uid);            NSLog(@"Renren accessToken: %@", resp.accessToken);            NSLog(@"Renren expiration: %@", resp.expiration);            // 第三方平台SDK源数据            NSLog(@"Renren originalResponse: %@", resp.originalResponse);        }    }];}

3.10  Kakao

  • 授权并获取用户信息(获取uid、access token及用户名等)
// 在需要进行获取用户信息的UIViewController中加入如下代码#import <UMSocialCore/UMSocialCore.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);        }    }];}

猜你喜欢

转载自blog.csdn.net/sundaysme/article/details/80471133
今日推荐