iOS客户端Demo体验

本文为您介绍号码认证功能Demo的运行步骤及示例代码,帮助您快速了解号码认证功能。更多SDK接入详情,请参见一键登录 > iOS客户端接入本机号码校验 > iOS客户端接入

步骤一:下载SDK

登录号码认证产品控制台,在概览页面右侧API&SDK区域,单击立即下载,进入API&SDK页面,根据页面提示下载并解压对应SDK。

步骤二:创建认证方案

登录号码认证服务控制台,新增号码认证方案,具体请参见创建认证方案

步骤三:填写密钥

SDK解压后随带Demo,获取Demo后,找到PrefixHeader.pch文件,将上一步认证方案生成的密钥设置在PNSATAUTHSDKINFO字段。

09

步骤四:更换工程签名文件

10

示例代码

引用头文件

引用后开始接入。

import <ATAuthSDK/ATAuthSDK.h>

初始化SDK

[
[TXCommonHandler sharedInstance] setAuthSDKInfo:authSDKInfo
  complete:^(NSDictionary * _Nonnull resultDic) {
  NSLog(@"设置密钥结果:%@", resultDic);
}
];

一键登录

该接口会产生多次回调,授权页的点击事件也通过该回调抛出。

返回600000返回码时即获取到Token。中国移动、中国联通、中国电信三大运营商Token的有效时间分别为2分钟、30分钟、10分钟,建议获取到之后立即使用。

TXCustomModel *model = [[TXCustomModel alloc] init]
__weak typeof(self) weakSelf = self;
[[TXCommonHandler sharedInstance] getLoginTokenWithTimeout:3.0
                                                controller:self
                                                     model:model
                                                  complete:^(NSDictionary * _Nonnull resultDic) {
    NSString *resultCode = [resultDic objectForKey:@"resultCode"];
    if ([PNSCodeLoginControllerPresentSuccess isEqualToString:resultCode]) {
        NSLog(@"授权页拉起成功回调:%@", resultDic);
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    } else if ([PNSCodeLoginControllerClickCancel isEqualToString:resultCode] ||
               [PNSCodeLoginControllerClickChangeBtn isEqualToString:resultCode] ||
               [PNSCodeLoginControllerClickLoginBtn isEqualToString:resultCode] ||
               [PNSCodeLoginControllerClickCheckBoxBtn isEqualToString:resultCode] ||
               [PNSCodeLoginClickPrivacyAlertView isEqualToString:resultCode] ||
               [PNSCodeLoginPrivacyAlertViewClickContinue isEqualToString:resultCode] ||
               [PNSCodeLoginPrivacyAlertViewClose isEqualToString:resultCode]) {
        NSLog(@"页面点击事件回调:%@", resultDic);
    }else if([PNSCodeLoginControllerClickProtocol isEqualToString:resultCode] ||
             [PNSCodeLoginPrivacyAlertViewPrivacyContentClick isEqualToString:resultCode]){
        NSLog(@"页面点击事件回调:%@", resultDic);
        NSString *privacyUrl = [resultDic objectForKey:@"url"];
        NSString *privacyName = [resultDic objectForKey:@"urlName"];
        NSLog(@"如果TXCustomModel的privacyVCIsCustomized设置成YES,则SDK内部不会跳转协议页,需要自己实现");
        if(model.privacyVCIsCustomized){
            PrivacyWebViewController *controller = [[PrivacyWebViewController alloc] initWithUrl:privacyUrl andUrlName:privacyName];
            controller.isHiddenNavgationBar = NO;
            UINavigationController *navigationController = weakSelf.navigationController;
            if (weakSelf.presentedViewController) {
                //如果授权页成功拉起,这个时候则需要使用授权页的导航控制器进行跳转
                navigationController = (UINavigationController *)weakSelf.presentedViewController;
            }
            [navigationController pushViewController:controller animated:YES];
        }
    } else if ([PNSCodeLoginControllerSuspendDisMissVC isEqualToString:resultCode]) {
        NSLog(@"页面点击事件回调:%@", resultDic);
        [[TXCommonHandler sharedInstance] cancelLoginVCAnimated:YES complete:nil];
    } else if ([PNSCodeSuccess isEqualToString:resultCode]) {
        NSLog(@"获取LoginToken成功回调:%@", resultDic);
        NSString *token = [resultDic objectForKey:@"token"];
        UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard];
        if ([token isKindOfClass:NSString.class]) {
            generalPasteboard.string = token;
        }
        NSLog(@"接下来可以拿着Token去服务端换取手机号,有了手机号就可以登录,SDK提供服务到此结束");
        //[weakSelf dismissViewControllerAnimated:YES completion:nil];
        [[TXCommonHandler sharedInstance] cancelLoginVCAnimated:YES complete:nil];
    } else {
        NSLog(@"获取LoginToken或拉起授权页失败回调:%@", resultDic);
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        //失败后可以跳转到短信登录界面
        PNSSmsLoginController *controller = [[PNSSmsLoginController alloc] init];
        controller.isHiddenNavgationBar = NO;
        UINavigationController *navigationController = weakSelf.navigationController;
        if (weakSelf.presentedViewController) {
            //如果授权页成功拉起,这个时候则需要使用授权页的导航控制器进行跳转
            navigationController = (UINavigationController *)weakSelf.presentedViewController;
        }
        [navigationController pushViewController:controller animated:YES];
    }
}];

本机号码校验

本机号码校验产生的Token需要和当前用户输入的手机号一起送至服务端进行对比,返回是否校验通过。

[[TXCommonHandler sharedInstance] getVerifyTokenWithTimeout:3.0
                                                       complete:^(NSDictionary * _Nonnull resultDic) {
        NSLog(@"获取本机号码校验Token返回:%@", resultDic);
        if ([PNSCodeSuccess isEqualToString:[resultDic objectForKey:@"resultCode"]]) {
            //NSString *token = [resultDic objectForKey:@"token"];
            NSString *token = [resultDic objectForKey:@"token"];
            UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard]; //快捷创建方法
            if ([token isKindOfClass:NSString.class]) {
                generalPasteboard.string = token;
            }
            NSString *text = @"获取Token成功,接下来可以拿着Token和用户输入手机号去服务端进行校验,SDK提供服务到此结束";
            [MBProgressHUD hideHUDForView:ws.view animated:YES];
            PNSToast(ws.view, text, 4.0);
        } else {
            [MBProgressHUD hideHUDForView:ws.view animated:YES];
            PNSToast(ws.view, [resultDic objectForKey:@"msg"], 3.0);
        }
    }];