版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.05.01 |
前言
很多移动应用都有集成微信、QQ、微博等第三方平台分享和登录等的需求,前段时间我又重新梳理了下这几部分。下面就再次梳理一遍。我们这里用的是第三方ShareSDK实现第三方登录。
详述三方登录流程
一、注册
这里的注册就很多了,如果你要利用QQ、微信或者微博等第三方平台进行登录,首先要到这些第三方平台上注册,获取APPKey等授权参数;同时也要在ShareSDK三方平台上注册。这里只给出ShareSDK的地址。
二、集成
集成也有两种方法,一种就是在Mob官网上下载SDK手动的拖入到官网中,另外一种比较方便就是通过cocoapods进行集成。
pod 'ShareSDK3'
pod 'MOBFoundation'
pod 'ShareSDK3/ShareSDKPlatforms/QQ'
pod 'ShareSDK3/ShareSDKPlatforms/SinaWeibo'
pod 'ShareSDK3/ShareSDKPlatforms/WeChat'
三、代码中的使用
1. AppDelegate.m中进行注册
//首先引入头文件
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApi.h"
#import "WeiboSDK.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/**
* 设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到http://mob.com/login 登录后台进行应用注册,
* 在将生成的AppKey传入到此方法中。
* 方法中的第二个第三个参数为需要连接社交平台SDK时触发,
* 在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。
* 如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。
*/
//第三方登录
[ShareSDK registerApp:@"这里是ShareSDK的AppId"
activePlatforms:@[
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeWechat),
@(SSDKPlatformTypeQQ),
] onImport:^(SSDKPlatformType platformType) {
switch (platformType) {
case SSDKPlatformTypeSinaWeibo:
[ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class]];
break;
case SSDKPlatformTypeQQ:
[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
break;
default:
break;
}
} onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
switch (platformType) {
case SSDKPlatformTypeSinaWeibo:
[appInfo SSDKSetupSinaWeiboByAppKey:@"这里是微博的AppKey"
appSecret:@"这里是微博的AppSecret"
redirectUri:@"这里是微博的RedirectURL"
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId: @"这里是微信的AppKey"
appSecret: @"这里是微信的AppSecret"];
break;
case SSDKPlatformTypeQQ:
[appInfo SSDKSetupQQByAppId: @"这里是QQ的AppId"
appKey: @"这里是QQ的AppKey"
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
return YES;
}
2. 配置URL跳转
在info.plist中进行跳转设置。
3. 设置白名单
这里有两种方式,一种是直接配置info.plist中的键值对。
另外一种就是info.plist--open as source code 直接加源码。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<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>mqzoneopensdk</string>
</array>
4. 在需要登录的地方实现登录的代码逻辑。
//这里实现登录逻辑
JJLoginVC.m
- (void)loginView:(ZBLoginView *)loginView buttonType:(ZBLoginViewButtonType)buttonType
{
//检查网络
if (![ZBNetworkReachabilityTool sharedNetworkReachabilityTool].isReachable) {
[self showNotice:@"请检查网络"];
return;
}
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorView.center = self.view.center;
[self.view addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
switch (buttonType) {
//微博
case ZBLoginViewButtonTypeWeiBoLogin:
{
[SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeSinaWeibo onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
//这里做的是,拿到三方的回调数据,然后根据这些数据向自己的服务器请求注册登录。
[self requestLogin:user loginType:SSDKPlatformTypeSinaWeibo];
} onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
if (state == SSDKResponseStateSuccess) {
NSLog(@"登录成功");
}
[self.activityIndicatorView stopAnimating];
[self.activityIndicatorView setHidesWhenStopped:YES];
}];
}
break;
//微信
case ZBLoginViewButtonTypeWechatLogin:
{
[SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeWechat onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
[self requestLogin:user loginType:SSDKPlatformTypeWechat];
} onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
if (state == SSDKResponseStateSuccess) {
NSLog(@"登录成功");
}
[self.activityIndicatorView stopAnimating];
[self.activityIndicatorView setHidesWhenStopped:YES];
}];
}
break;
//QQ
case ZBLoginViewButtonTypeQQLogin:
{
[SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeQQ onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
[self requestLogin:user loginType:SSDKPlatformTypeQQ];
} onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
if (state == SSDKResponseStateSuccess) {
NSLog(@"登录成功");
}
[self.activityIndicatorView stopAnimating];
[self.activityIndicatorView setHidesWhenStopped:YES];
}];
}
break;
default:
break;
}
}
//三方登录请求接口
- (void)requestLogin:(SSDKUser *)user loginType:(SSDKPlatformType)loginType
{
NSMutableDictionary *paramDict = [self gainParameterWithUser:user loginType:loginType];
//在这个方法里面调用自己服务器的后台登录接口,根据回调数据,选择合适的参数调用接口注册登录。
[self loginWithThirdPartyWithParam:paramDict];
}
- (NSMutableDictionary *)gainParameterWithUser:(SSDKUser *)user loginType:(SSDKPlatformType)loginType
{
NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
NSString *logAuthTypeType = nil;
switch (loginType) {
case SSDKPlatformTypeWechat:
{
logAuthTypeType = @"2";
}
break;
case SSDKPlatformTypeQQ:
{
logAuthTypeType = @"3";
}
break;
case SSDKPlatformTypeSinaWeibo:
{
logAuthTypeType = @"4";
}
break;
default:
break;
}
switch (user.gender) {
case SSDKGenderMale:
[paramDict setObject:@"1" forKey:@"userSex"];
break;
case SSDKGenderFemale:
[paramDict setObject:@"2" forKey:@"userSex"];
break;
case SSDKGenderUnknown:
[paramDict setObject:@"0" forKey:@"userSex"];
break;
default:
break;
}
//生日
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateStr = [dateFormatter stringFromDate:user.birthday];
[paramDict setObject:user.uid?:@"" forKey:@"authOpenid"]; //openid
[paramDict setObject:logAuthTypeType?:@"" forKey:@"authType"]; //authorType
[paramDict setObject:[user.rawData objectForKey:@"unionid"]?:@"" forKey:@"authUnionid"]; //authUnionid
// [paramDict setObject:[user.rawData objectForKey:@"age"]?:@"" forKey:@"userAge"]; //userAge
[paramDict setObject:user.icon?:@"" forKey:@"userAvatar"]; //userAvatar
// [paramDict setObject:dateStr?:@"" forKey:@"userBirthday"]; //userBirthday
[paramDict setObject:[user.rawData objectForKey:@"city"]?:@"" forKey:@"userCity"]; //userCity
[paramDict setObject:user.nickname?:@"" forKey:@"userNickname"]; //userNickname
[paramDict setObject:[user.rawData objectForKey:@"province"]?:@"" forKey:@"userProvince"]; //userProvince
[paramDict setObject:user.verifyReason?:@"" forKey:@"verifiedReason"]; //verifiedReason
return paramDict;
}
//在这里实现请求登录。
- (void)loginWithThirdPartyWithParam:(NSMutableDictionary *)paramDict
{
NSString *phoneLoginServer = [NSString stringWithFormat:@"%@%@",kDomainURL,kLoginThirdPartyLogin];
[[NetWorkManager manager] requestByPostNetworkWithServerUrl:phoneLoginServer parameters:paramDict success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} error:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error---%@",error);
}];
}
后记
这里就是三方登录的主要流程,根据开发文档或者其他资料慢慢集成,并不困难,按部就班即可,谢谢大家对我的支持,谢谢大家。五一长假就这么结束喽,写了好几天的博客,祝大家假日快乐。