美文网首页OC-开发案例收集
iOS开发 APP 集成本机号码一键注册功能

iOS开发 APP 集成本机号码一键注册功能

作者: 有O梦想的咸鱼 | 来源:发表于2020-11-25 17:24 被阅读0次

    项目中使用的是阿里的SDK,具体可以看阿里的文档:iOS 客户端接入
    我记录下项目中的代码部分:
    项目中使用的是授权页全屏模式,在需要弹授权页的控制器导入头文件:

    #import <ATAuthSDK/ATAuthSDK.h>
    

    然后定义一个属性:

    ///是否可以一键注册
    @property (nonatomic, assign) BOOL isCanUseOneKey;
    

    以下为代码部分:

    //开始状态置为YES,默认当前环境可以使用一键登录
        self.isCanUseOneKey = YES;
        __weak typeof(self) weakSelf = self;
        //环境检查,异步返回
        [[TXCommonHandler sharedInstance] checkEnvAvailableWithAuthType:PNSAuthTypeLoginToken
                                                               complete:^(NSDictionary * _Nullable resultDic) {
            NSLog(@"环境检查返回:%@", resultDic);
            weakSelf.isCanUseOneKey = [PNSCodeSuccess isEqualToString:[resultDic objectForKey:@"resultCode"]]; 
            if (weakSelf.isCanUseOneKey == NO) {//不能使用一键注册时跳转其他方式注册页面
          //其他注册方式
    
            } else {  
            //设置授权页UI          
                TXCustomModel *model = [[TXCustomModel alloc] init];
                model.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
                model.navColor = White_Color;
                NSDictionary *attributes = @{
                    NSForegroundColorAttributeName : Black_Color,
                    NSFontAttributeName : [UIFont systemFontOfSize:20.0]
                };
                model.navTitle = [[NSAttributedString alloc] initWithString:@"一键注册" attributes:attributes];
                model.navBackImage = [UIImage imageNamed:@"icon_nav_back_light"];
                model.logoImage = [UIImage imageNamed:@"applogo_160"];
                model.loginBtnText = [[NSAttributedString alloc] initWithString:@"本机号码一键注册" attributes:@{NSForegroundColorAttributeName:White_Color,NSFontAttributeName:[UIFont systemFontOfSize:18]}];
                UIImage *canImage = [UIImage imageNamed:@"home_screen_register_button"];
                UIImage *NOimage = [[UIImage alloc] changeImage:canImage tintColor:UIColorFromHex(0xDCDCDC)];
                model.loginBtnBgImgs = @[canImage,NOimage,canImage]; 
                model.changeBtnIsHidden = NO;
                model.changeBtnTitle = [[NSAttributedString alloc] initWithString:@"其他手机号注册" attributes:@{NSForegroundColorAttributeName:LightGray_Color,NSFontAttributeName:[UIFont systemFontOfSize:14]}]; 
                model.autoHideLoginLoading = NO;
                model.sloganIsHidden = YES;
                model.checkBoxWH = 24;
                model.checkBoxImages = @[[UIImage imageNamed:@"Icons_checkoff"],[UIImage imageNamed:@"Icons_checkon"]];
                NSURL *file = [[NSUserDefaults standardUserDefaults] URLForKey:yinsiPath];
                NSURL *file1 = [[NSUserDefaults standardUserDefaults] URLForKey:yonghuPath];
                NSString *string = file.absoluteString;
                NSString *string1 = file1.absoluteString;
                //自己的协议,加载服务器下载下来的
                model.privacyOne = @[@"隐私政策",string];
                model.privacyTwo = @[@"用户协议",string1];
                model.presentDirection = PNSPresentationDirectionRight;
                //调整UI
                model.logoFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
                    frame.size.width = 98;
                    frame.size.height = 99.5;
                    frame.origin.x = (superViewSize.width - 98) * 0.5;
                    frame.origin.y = superViewSize.height*1/5 ;
                    return frame;
                };
                model.loginBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
                    frame.size.width = 240;
                    frame.size.height = 48;
                    frame.origin.x = (superViewSize.width - 240) * 0.5;
                    frame.origin.y = superViewSize.height*1/5  + 99.5 + 10 + 20 +20;
                    return frame;
                };
                model.changeBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
                    frame.size.width = 240;
                    frame.size.height = 48;
                    frame.origin.x = (superViewSize.width - 240) * 0.5;
                    frame.origin.y = superViewSize.height*1/5  + 99.5 + 10 + 20 +20 + 48;
                    return frame;
                };
       
                __weak typeof(self) weakSelf = self;
                [[TXCommonHandler sharedInstance] getLoginTokenWithTimeout:3.0
                                                                controller:self
                                                                     model:model
                                                                  complete:^(NSDictionary * _Nonnull resultDic) {
                    NSString *code = [resultDic objectForKey:@"resultCode"];
                    if ([PNSCodeLoginControllerPresentSuccess isEqualToString:code]) {
                        //   [ProgressHUD showSuccess:@"弹起授权⻚成功"];
                    } else if ([PNSCodeLoginControllerClickCancel isEqualToString:code]) {
                        //  [ProgressHUD showSuccess:@"点击了授权⻚的返回"];
                    } else if ([PNSCodeLoginControllerClickChangeBtn isEqualToString:code]) {
                        //    [ProgressHUD showSuccess:@"点击切换其他注册⽅式按钮"];
                        dispatch_async(dispatch_get_main_queue(), ^{//注销授权页,建议用此方法,对于移动卡授权页的消失会清空一些数据
                            [[TXCommonHandler sharedInstance] cancelLoginVCAnimated:YES complete:nil];
                        });
                   //其他注册方式
         
                    } else if ([PNSCodeLoginControllerClickLoginBtn isEqualToString:code]) {
                        if ([[resultDic objectForKey:@"isChecked"] boolValue] == YES) {
                            //[ProgressHUD showSuccess:@"点击了登录按钮,check box选 中,SDK内部接着会去获取登陆Token"];
                        } else {
                            // [ProgressHUD showSuccess:@"点击了登录按钮,check box选 中,SDK内部不会去获取登陆Token"];
                        }
                    } else if ([PNSCodeLoginControllerClickCheckBoxBtn isEqualToString:code]) {
                        //  [ProgressHUD showSuccess:@"点击check box"];
                    } else if ([PNSCodeLoginControllerClickProtocol isEqualToString:code]) {
                        //  [ProgressHUD showSuccess:@"点击了协议富⽂本"];
                    } else if ([PNSCodeSuccess isEqualToString:code]) {
                        //点击按钮获取注册Token成功回调
                        NSString *token = [resultDic objectForKey:@"token"];
                        QYLog(@"一键注册token = %@",token);
                        //下⾯拿Token去自己的服务器换⼿机号
    
                    } else {
                        [[TXCommonHandler sharedInstance] hideLoginLoading];
                        [Util toastMessage:@"请检查是否插入SIM卡并打开蜂窝网络" delay:2.0];
                    }
                }];
            };
        }];
    

    到这里一键注册代码部分就完成了。
    授权页的UI绝大多数是支持更改的,主要看自己项目需求然后在对照属性去更改。

    相关文章

      网友评论

        本文标题:iOS开发 APP 集成本机号码一键注册功能

        本文链接:https://www.haomeiwen.com/subject/czuniktx.html