Try the iOS client demo

更新时间:
复制 MD 格式

This topic describes how to run the Phone Number Verification demo and provides sample code to help you get started. For more information about how to integrate the SDK, see one-click login > Integrate the iOS client and mobile number verification > Integrate the iOS client.

Step 1: Download the SDK

Log on to the Phone Number Verification Service console. On the Overview page, find the API & SDK section on the right and click Download Now. Follow the on-screen instructions to download the required SDK.

Step 2: Create an authentication scheme

Log in to the Phone Number Verification Service console and create a new authentication scheme. For details, see Create an authentication scheme.

Step 3: Set the key

The extracted SDK package includes a demo. In the demo, locate the PrefixHeader.pch file and set the PNSATAUTHSDKINFO field to the key from your authentication scheme.

//
// PrefixHeader.pch
// ATAuthSceneDemo
//
// xxx
// xxx

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "xxx"
#import "xxx"
#import "xxx"
#import <xxx>
#import "xxx"

xxx

#define PNSATAUTHSDKINFOKEY @"ATAuthSDKInfoKey"
#define PNSATAUTHSDKINFO @"Set this to your key, which you can copy from the console."

#endif /* PrefixHeader_pch */

Step 4: Configure project signing

Open the demo project in Xcode. Under TARGETS, select ATAuthSceneDemo, and click the Signing & Capabilities tab. In the Signing section, clear the Automatically manage signing checkbox, change the Bundle Identifier to your app's identifier, and then, in the iOS section, configure your Provisioning Profile, Team, and Signing Certificate.

Sample code

Import the header file

import <ATAuthSDK/ATAuthSDK.h>

Initialize the SDK

[
[TXCommonHandler sharedInstance] setAuthSDKInfo:authSDKInfo
  complete:^(NSDictionary * _Nonnull resultDic) {
  NSLog(@"Key setup result: %@", resultDic);
}
];

One-click login

This API triggers multiple callbacks, which also return user interactions from the authorization page.

A return code of 600000 indicates a successful token retrieval. The validity periods for tokens from China Mobile, China Unicom, and China Telecom are 2 minutes, 30 minutes, and 10 minutes, respectively. Use the token immediately after receiving it.

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(@"Callback for successfully presenting the authorization page: %@", 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(@"Callback for a page interaction event: %@", resultDic);
    }else if([PNSCodeLoginControllerClickProtocol isEqualToString:resultCode] ||
             [PNSCodeLoginPrivacyAlertViewPrivacyContentClick isEqualToString:resultCode]){
        NSLog(@"Callback for a page interaction event: %@", resultDic);
        NSString *privacyUrl = [resultDic objectForKey:@"url"];
        NSString *privacyName = [resultDic objectForKey:@"urlName"];
        NSLog(@"If privacyVCIsCustomized in TXCustomModel is set to YES, the SDK does not open the terms of service page. You must implement the page navigation yourself.");
        if(model.privacyVCIsCustomized){
            PrivacyWebViewController *controller = [[PrivacyWebViewController alloc] initWithUrl:privacyUrl andUrlName:privacyName];
            controller.isHiddenNavgationBar = NO;
            UINavigationController *navigationController = weakSelf.navigationController;
            if (weakSelf.presentedViewController) {
                //If the authorization page is presented, use its navigation controller for the transition.
                navigationController = (UINavigationController *)weakSelf.presentedViewController;
            }
            [navigationController pushViewController:controller animated:YES];
        }
    } else if ([PNSCodeLoginControllerSuspendDisMissVC isEqualToString:resultCode]) {
        NSLog(@"Callback for a page interaction event: %@", resultDic);
        [[TXCommonHandler sharedInstance] cancelLoginVCAnimated:YES complete:nil];
    } else if ([PNSCodeSuccess isEqualToString:resultCode]) {
        NSLog(@"Callback for a successful login token retrieval: %@", resultDic);
        NSString *token = [resultDic objectForKey:@"token"];
        UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard];
        if ([token isKindOfClass:NSString.class]) {
            generalPasteboard.string = token;
        }
        NSLog(@"Use this token on your server to retrieve the user's mobile number and complete the login. This concludes the SDK's role in the process.");
        //[weakSelf dismissViewControllerAnimated:YES completion:nil];
        [[TXCommonHandler sharedInstance] cancelLoginVCAnimated:YES complete:nil];
    } else {
        NSLog(@"Callback for failure to get the login token or present the authorization page: %@", resultDic);
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        //If the process fails, you can redirect the user to an SMS login screen.
        PNSSmsLoginController *controller = [[PNSSmsLoginController alloc] init];
        controller.isHiddenNavgationBar = NO;
        UINavigationController *navigationController = weakSelf.navigationController;
        if (weakSelf.presentedViewController) {
            //If the authorization page is presented, use its navigation controller for the transition.
            navigationController = (UINavigationController *)weakSelf.presentedViewController;
        }
        [navigationController pushViewController:controller animated:YES];
    }
}];

Mobile number verification

Send the token from the mobile number verification and the user-entered mobile number to your server. Your server then compares them to complete the verification.

[[TXCommonHandler sharedInstance] getVerifyTokenWithTimeout:3.0
                                                       complete:^(NSDictionary * _Nonnull resultDic) {
        NSLog(@"Response for the mobile number verification token request: %@", 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 obtained successfully. You can now send the token and the user-entered mobile number to your server for verification. This concludes the SDK's role in the process.";
            [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);
        }
    }];