iOS接入阿里云一键登录,号码认证 - yucaijiang

时间:2024-02-23 19:01:01

iOS接入阿里云一键登录,号码认证

我接入这个的时候,网上可用代码找不到,sdk文档也写的比较坑,各种错别字就不说了。

先说说比较坑的:1.  这个官方sdk带的demo下载下来是无法运行的,需要手动替换里面的sdk包,因为demo里面的不是release模式。

                        2. 然后换了sdk之后发现ATAuthSDK.bundle和TYRZResource.bundle这两个文件找不到,需要手动从release文件将他们拖出来放到工程里面。

 

到这里demo能运行了。

 

好接下来,需要在阿里云后台注册app,填好bundelid就行了。在自己工程里面也要按照上面的步骤将sdk导入,其他配置按照接入文档操作即可。

代码部分:

第一步:首次判断当前设备网络环境,有没有运营商网络 

        self.isEnable = [[TXCommonHandler sharedInstance] checkSyncGatewayVerifyEnable:nil];//判断当前设备是否有移动运营商网络

        if (!self.isEnable) {

            [self.view hideToastActivity];

            [KPToast showToastWithMessage:@"设备无移数据网络 或 获获取供应商信息失败"];

            return;

        }else{

            [self getLoginMobileAndToken];

        }

 

第二部:调用阿里云sdk

#pragma mark ------ 调用一键登录sdk

- (void)getLoginMobileAndToken{

    

    TXCustomModel *modelNew = [[TXCustomModel alloc] init];

    modelNew.navTitle = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSForegroundColorAttributeName : UIColor.blackColor,NSFontAttributeName : [UIFont systemFontOfSize:24.0]}];

    modelNew.navBackImage = [UIImage imageNamed:@"icon_nav_back_gray"];

    

    //logo

    modelNew.logoImage = [UIImage imageNamed:@"pf_login_icon"];

    modelNew.logoIsHidden = NO;

    modelNew.logoWidth = 200;

    modelNew.logoHeight = 90.0;

    modelNew.logoTopOffetY = self.ratio * 50.0;

    

    //xxxx运营商提供认证服务

    modelNew.sloganIsHidden = NO;

    modelNew.sloganText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@提供号码认证服务",[TXCommonUtils getCurrentCarrierName]] attributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x333333),NSFontAttributeName : [UIFont systemFontOfSize:16.0]}];

    modelNew.sloganTopOffetY = self.ratio * 170.0;

    

    //手机号码

    modelNew.numberColor = UIColorFromRGB(0x333333);

    modelNew.numberFont = [UIFont systemFontOfSize:20.0];

    modelNew.numberTopOffetY = self.ratio * 200.0;

   

    //登录按钮

    modelNew.loginBtnText = [[NSAttributedString alloc] initWithString:@"一键登录" attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor,NSFontAttributeName : [UIFont systemFontOfSize:20.0]}];

    modelNew.loginBtnTopOffetY = self.ratio * 270;

    modelNew.loginBtnHeight = 50.0;

    modelNew.loginBtnLRPadding = 18.0;

//    modelNew.loginBtnBgImgs = @[@"pf_loginBtn_bac",@"pf_loginBtn_bac",@"pf_loginBtn_bac"];

    

    //底部协议

    modelNew.privacyColors = @[UIColor.lightGrayColor,UIColorFromRGB(0x3c99fa)];

    modelNew.privacyBottomOffetY = self.ratio * 25.0;

    modelNew.privacyAlignment = NSTextAlignmentCenter;

    modelNew.privacyLRPadding = 8.0;

    modelNew.privacyFont = [UIFont systemFontOfSize:12.0];

    

    modelNew.checkBoxIsHidden = NO;

    modelNew.checkBoxIsChecked = YES;

    modelNew.checkBoxWH = 15.0;

    

    //切换到其他方式

    modelNew.changeBtnTitle = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSForegroundColorAttributeName : UIColor.orangeColor,NSFontAttributeName : [UIFont systemFontOfSize:18.0]}];

    modelNew.changeBtnIsHidden = YES;//隐藏

    modelNew.changeBtnTopOffetY = self.ratio * 344;

    

    __weak KPLoginNewViewController *weakSelf = self;

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

        [self.view hideToastActivity];

        dispatch_async(dispatch_get_main_queue(), ^{

            NSString *code = [resultDic valueForKey:@"resultCode"];

            if ([code isEqualToString:TX_Auth_Result_Success]) {

                

            }else if ([code isEqualToString:TX_Login_SSO_Action]) {//成功登录

                [weakSelf dismissViewControllerAnimated:YES completion:nil];

                NSString *token = [resultDic valueForKey:@"token"];

                NSLog(@"运营商token===%@",token);

                // 可以去请求服务端getMobile接口,用token换取手机号码啦

                    }else{

                        [weakSelf.view makeToast:@"登录失败"];

                    }

                } failure:^(NSString *errorStr, NSString *errorCode) {

                    [weakSelf.view makeToast:@"登录失败"];

                }];

                

            }else if ([code isEqualToString:TX_Login_Change_Action]) {//切换到其他方式

 

            }else if ([code isEqualToString:TX_Login_Return_Action] || [code isEqualToString:TX_Login_AuthPage_Show_Success]) {

                

            }else {

                [weakSelf dismissViewControllerAnimated:YES completion:nil];

                NSLog(@"getLoginTokenWithController fail,resultDic = %@",resultDic);

                [KPToast showToastWithMessage:@"获取登录Token失败"];

            }

#ifdef DEBUG

            NSLog(@"getLoginTokenWithController,resultDic = %@",[self jsonStrFromDic:resultDic]);

#endif

        });

    }];

}

 

代码就这么多,阿里云第三方页面可以高度定制,灵活更改,有不懂的联系553502397@qq.com