登录  /  免费注册

IOS-一键登录

KB: 121050

 · 

更新时间:2019-06-17 17:19:45 

一键登录 iOS SDK 开发接入文档

一键登录流程

  • 调用SDK初始化接口checkGatewayVerifyEnable
  • (可选,唤起授权页更快)调用SDK预取号接口getLoginNumberWithTimeout
  • 调用SDK登录Token接口getLoginTokenWithController
  • 调用业务方服务端API取号接口

getLoginTokenWithController接口默认包含预取号接口的调用,所以授权页面唤起时间偏慢些,如果先调用getLoginNumberWithTimeout接口,但是业务方得保障getLoginNumberWithTimeout回调成功后,再调用getLoginTokenWithController接口,授权页面唤起时间就偏快

SDK说明

阿里通信号码认证服务iOS SDK目前主要提供功能:

  • SDK版本号;
  • 设备网络初始化接口,检测设备是否已开启蜂窝数据网络,这部是号码服务的前置条件
  • 号码认证AuthToken接口(支持移动,电信,联通三大运营商),是请求认证的凭证,可自定义超时时间,默认3.0 s,单位 s;
  • 一键登录LoginNumber接口(支持移动,电信,联通三大运营商),是预取号缓存接口,为LoginToken接口缓存前置条件,授权页唤起更快;
  • 一键登录LoginToken接口(支持移动,电信,联通三大运营商),是请求登录的凭证,可自定义超时时间,默认3.0 s,单位 s;

目前只提供静态库.framework形式。

项目设置(非常重要!!)

App项目中,需要操作如下:

  • Targets->General->Linked Frameworks and Libraries中添加主库,ATAuthSDK.framework

  • Targets->BuildSettings中,设置Enable BitCodeNOOther Linker Flags增加-ObjC一定要添加此选项,注意是大写C,不是小写c,否则工程运行起来会crash!!

  • 由于某运营商还存在http请求,所以请确保Targets->InfoATS开关已开启,并设置为YES

  • 主项目右键添加下ATAuthSDK.frameworkATAuthSDK.bundleTYRZResource.bundle资源文件,否则一键登录授权页面图片或icon显示不出来;

主接口简介

  1. /*
  2. * 函数名:sharedInstance
  3. * 参数:无
  4. * 返回:单例
  5. */
  6. + (instancetype _Nonnull )sharedInstance;

  7. /*
  8. * 函数名:getVersion
  9. * 参数:无
  10. * 返回:字符串,sdk版本号
  11. */
  12. - (NSString *_Nonnull)getVersion;

  13. /*
  14. * 函数名:checkGatewayVerifyEnable
  15. * 参数:phoneNumber,手机号码,非必传,号码认证且双sim卡时必须传入待验证的手机号码!!,一键登录时设置为nil即可
  16. * 返回:BOOL值,YES表示网关认证所需的蜂窝数据网络已开启,NO表示未开启,只有YES才能保障后续服务
  17. */
  18. - (BOOL)checkGatewayVerifyEnable:(NSString *_Nullable)phoneNumber;

  19. /*
  20. * 函数名:getLoginNumberWithTimeout,一键登录预取号
  21. * 参数:
  22. timeout:接口超时时间,单位s,默认3.0s,值为0.0时采用默认超时时间
  23. * 返回:字典形式
  24. * resultCode:6666-成功,5555-超时,4444-失败,3344-参数异常,2222-无网络,1111-无SIM卡,6668-登录按钮事件,6669-切换到其他方式按钮事件
  25. * msg:文案或错误提示
  26. */

  27. - (void)getLoginNumberWithTimeout:(NSTimeInterval )timeout complete:(void (^_Nullable)(NSDictionary * _Nonnull resultDic))complete;

  28. /*
  29. * 函数名:getLoginTokenWithController,一键登录token
  30. * 参数:
  31. vc:当前vc容器,用于一键登录授权页面切换
  32. model:自定义授权页面选项,可为nil,采用默认的授权页面,具体请参考TXCustomModel.h文件
  33. timeout:接口超时时间,单位s,默认3.0s,值为0.0时采用默认超时时间
  34. * 返回:字典形式
  35. * resultCode:6666-成功,5555-超时,4444-失败,3344-参数异常,2222-无网络,1111-无SIM卡,6668-登录按钮事件,6669-切换到其他方式按钮事件
  36. * token:一键登录token
  37. * msg:文案或错误提示
  38. */

  39. - (void)getLoginTokenWithController:(UIViewController *_Nonnull)vc model:(TXCustomModel *_Nullable)model timeout:(NSTimeInterval )timeout complete:(void (^_Nullable)(NSDictionary * _Nonnull resultDic))complete;

IOS授权页页面细则

接口返回编码resultCode说明

  • 6666,请求成功
  • 5555,请求超时
  • 4444,请求失败,具体错误见msg文案,一般是运营商的异常
  • 1111,设备无SIM卡
  • 2222,设备无移动蜂窝网络
  • 3344,参数为空或异常
  • 6668,一键登录授权页面,登录按钮被触发事件
  • 6669,一键登录授权页面,切换账号按钮被触发事件

接入demo用例

  • 一键登录接入(缓存版本)

  1. // 1. 初始化接口,判断环境是否支持一键登录(参数为nil),为YES,才能执行2步骤
  2. self.isEnable = [[TXCommonHandler sharedInstance] checkGatewayVerifyEnable:nil];

  3. // 2. 预取号缓存,便于授权页面迅速拉起,0等待
  4. __weak TXNumberCheckViewController *weakSelf = self;
  5. [[TXCommonHandler sharedInstance] getLoginNumberWithTimeout:0.0 complete:^(NSDictionary * _Nonnull resultDic) {
  6. NSString *code = [resultDic valueForKey:@"resultCode"];
  7. NSString *msg = [resultDic valueForKey:@"msg"];

  8. dispatch_async(dispatch_get_main_queue(), ^{
  9. if ([code isEqualToString:TX_Auth_Result_Success]) {
  10. NSLog(@"getLoginNumberWithTimeout success");
  11. weakSelf.resultTextView0.text = @"预取号成功";

  12. // 3.1 拉起授权页面
  13. TXCustomModel *modelNew = [[TXCustomModel alloc] init];
  14. [[TXCommonHandler sharedInstance] getLoginTokenWithController:self model:modelNew timeout:self.time complete:^(NSDictionary * _Nonnull resultDic) {

  15. // 具体参考非缓存版本代码;

  16. };

  17. }
  18. else {
  19. NSLog(@"getLoginNumberWithTimeout fail,resultDic = %@",resultDic);
  20. weakSelf.resultTextView0.text = [NSString stringWithFormat:@"预取号失败,%@",msg];
  21. }
  22. });
  23. }];

  24. 3步骤,之前要确保getLoginNumberWithTimeout接口成功回调,可以block嵌套调用,也可以分开调用!!)
  25. // 3.2 拉起授权页面
  26. TXCustomModel *modelNew = [[TXCustomModel alloc] init];
  27. [[TXCommonHandler sharedInstance] getLoginTokenWithController:self model:modelNew timeout:self.time complete:^(NSDictionary * _Nonnull resultDic) {
  28. // 具体参考非缓存版本代码;
  29. };
  • 一键登录接入(无缓存版本)

  1. self.isEnable = [[TXCommonHandler sharedInstance] checkGatewayVerifyEnable:nil];
  2. if (!self.isEnable) {
  3. self.resultTextView.text = @"设备无移动数据网络 或 获取供应商信息失败";
  4. return;
  5. }

  6. // 2. 获取登录token,用于3步骤登录取号接口参数
  7. __weak TXNumberCheckViewController *weakSelf = self;

  8. // 授权页面属性定制
  9. TXCustomModel *modelNew = [[TXCustomModel alloc] init];

  10. modelNew.navColor = UIColor.whiteColor;
  11. modelNew.navTitle = [[NSAttributedString alloc] initWithString:@"一键登录" attributes:@{NSForegroundColorAttributeName : UIColor.blackColor,NSFontAttributeName : [UIFont systemFontOfSize:18.0]}];
  12. //modelNew.navTitle = [[NSAttributedString alloc] initWithString:@"一键登录" attributes:@{NSForegroundColorAttributeName : UIColorFromHex(0x333333),NSFontAttributeName : [UIFont systemFontOfSize:18.0]}];
  13. //UIBarButtonItem *moreControl = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_light"] style:UIBarButtonItemStylePlain target:self action:@selector(showMoreAction:)];
  14. UIBarButtonItem *moreControl = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(showMoreAction:)];
  15. moreControl.tintColor = UIColor.grayColor;
  16. modelNew.navMoreControl = moreControl;

  17. //modelNew.logoImage = [self imageWithColor:UIColor.orangeColor size:CGSizeMake(300.0, 300.0) isRoundedCorner:YES];
  18. modelNew.logoImage = [UIImage imageNamed:@"altx_logo"];
  19. modelNew.logoIsHidden = NO;
  20. modelNew.logoWidth = 200;
  21. modelNew.logoHeight = 90.0;
  22. modelNew.logoTopOffetY = self.ratio * 32.0;

  23. modelNew.sloganIsHidden = NO;
  24. modelNew.sloganText = [[NSAttributedString alloc] initWithString:@"一键登录slogan文案" attributes:@{NSForegroundColorAttributeName : UIColor.orangeColor,NSFontAttributeName : [UIFont systemFontOfSize:16.0]}];
  25. modelNew.sloganTopOffetY = self.ratio * 150.0;

  26. //modelNew.numberColor = UIColor.orangeColor;
  27. //modelNew.numberSize = 20.0;
  28. //modelNew.numberTopOffetY = self.ratio * 200.0;

  29. //UIImage *normalImage = [self imageWithColor:UIColor.orangeColor size:CGSizeMake(300.0, 45.0) isRoundedCorner:YES];
  30. //UIImage *disableImage = [self imageWithColor:UIColor.grayColor size:CGSizeMake(300.0, 45.0) isRoundedCorner:YES];
  31. //UIImage *lightedImage = [self imageWithColor:UIColor.greenColor size:CGSizeMake(300.0, 45.0) isRoundedCorner:YES];
  32. //modelNew.loginBtnBgImgs = @[normalImage,disableImage,lightedImage];
  33. modelNew.loginBtnTitle = @"一键登录";
  34. modelNew.loginBtnTitleColor = UIColor.whiteColor;
  35. modelNew.loginBtnTopOffetY = self.ratio * 270;

  36. modelNew.privacyOne = @[@"流量App使用方法1",@"https://www.taobao.com/"];
  37. modelNew.privacyTwo = @[@"流量App使用方法2",@"https://www.baidu.com/"];
  38. modelNew.privacyColors = @[UIColor.darkTextColor,UIColor.blueColor];
  39. modelNew.privacyBottomOffetY = self.ratio * 15.0;
  40. //modelNew.checkBoxIsChecked = YES;
  41. //UIImage *checkedImg = [self imageWithColor:UIColor.orangeColor size:CGSizeMake(45.0, 45.0) isRoundedCorner:NO];
  42. //UIImage *unCheckedImg = [self imageWithColor:UIColor.grayColor size:CGSizeMake(45.0, 45.0) isRoundedCorner:NO];
  43. //modelNew.checkBoxImages = @[unCheckedImg,checkedImg];

  44. modelNew.changeBtnTitle = [[NSAttributedString alloc] initWithString:@"切换到其他方式" attributes:@{NSForegroundColorAttributeName : UIColor.blueColor,NSFontAttributeName : [UIFont systemFontOfSize:18.0]}];
  45. modelNew.changeBtnIsHidden = NO;
  46. modelNew.changeBtnTopOffetY = self.ratio * 344;

  47. modelNew.customViewBlock = ^(UIView * _Nonnull superCustomView) {
  48. UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(40, self.ratio * 430, TX_SCREEN_WIDTH - 2 * 40, self.ratio * 80)];
  49. otherView.backgroundColor = UIColor.lightGrayColor;

  50. [superCustomView addSubview:otherView];
  51. };

  52. [[TXCommonHandler sharedInstance] getLoginTokenWithController:self model:modelNew timeout:self.time complete:^(NSDictionary * _Nonnull resultDic) {

  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. NSString *code = [resultDic valueForKey:@"resultCode"];
  55. if ([code isEqualToString:TX_Auth_Result_Success]) {
  56. // 授权页面成功唤起
  57. }
  58. else if ([code isEqualToString:TX_Login_SSO_Action]) {
  59. // 授权页面销毁
  60. [weakSelf dismissViewControllerAnimated:YES completion:nil];

  61. NSString *token = [resultDic valueForKey:@"token"];
  62. // 3.成功,请求业务服务端API,进行号码获取
  63. }
  64. else if ([code isEqualToString:TX_Login_Change_Action]) {
  65. // 授权页面销毁
  66. [weakSelf dismissViewControllerAnimated:YES completion:nil];

  67. // 4.切换账号,可切换到业务方自己的认证方法
  68. }
  69. else {
  70. // 授权页面销毁
  71. [weakSelf dismissViewControllerAnimated:YES completion:nil];

  72. // 5.失败,切换到业务方自己的认证方法
  73. }
  74. });
  75. }];

常见iOS问题

  • 初始化接口checkGatewayVerifyEnable一直返回NO
    • 排查顺序:1、手机sim卡是否被激活或欠费,2、手机设备蜂窝数据是否开启,3、App的网络权限是否开启,4、设备是否有代理,5、客户端上是否创建了方案号,6、创建方案号中的bundleId一定要与项目中使用的保持一致,7,最后再提供bundleId给我们进行查询日志;
  • 移动卡返回【103101,错误的请求签名】
    • 执行下[NSBundle mainBundle].localizedInfoDictionary语句,如果不为nil,则有国际本地化逻辑,这样的情况,目前有两种方法修复:1,如果.strings文件内容为空且无用,删除即可;2,如果.strings文件必须要使用,则需要在主工程中所有.strings文件中增加CFBundleIdentifier = "bundleId名称"
  • 登录token存在失败吗?
    • 肯定存在的,如果是偶现,可以理解,因为比如网络波动导致网关断开、网络不可用、供应商服务端异常,业务方服务端异常等因素有关;如果是持续出现,一般需要运营商协助排查;
  • iPhone双卡双待,出现5555错误码,超时失败?
    • 此设备问题,在切换上网卡或更换sim卡,会出现设备已经呈现4G网络,但实际Safari是打不开网页的状态,也就是说实际网络并没有准备好,所以在请求认证前,请先确保Safari能正常打开网页。
  • 什么情况下会出现异常错误码4444?
    • 错误码4444,一般发生在运营商端异常。
  • 一键登录服务一般只4G或者4G+Wifi情况下,如果是3G、2G会怎么样?
    • 中国移动支持2G/3G/4G、中国联通支持3G/4G、中国电信支持4G,但2G和3G网络下接口请求失败或超时概率稍高。
  • 经常超时?
    • 首先确认sim卡不欠费,再确认手机网络是否正常,通过Safari打开某个网址验证为准。
    • 其次确认超时时间是否设置正确,单位是s。
    • 2G和3G网络下接口请求失败或超时概率稍高。
  • 返回4433错误码,参数为空
    • 首先确认是否开通了方案,再检查App的bundleId与客户端新建方案中的bundlId是否一致。