All Products
Search
Document Center

Device Risk Detection SDK for iOS (2020)

Last Updated: Aug 16, 2021

This topic describes how to integrate and use Device Risk Detection SDK for iOS.

Prerequisites

  • The version of the iOS operating system meets the following requirements:

    iOS 9.0 or later

  • The relevant dependencies are added.

    1. Download Device Risk Detection SDK for iOS and create an appkey in the Fraud Detection console. The SDK package is a standard static framework package for Xcode.

    2. Reference the downloaded SDK package in Xcode.

    3. Select the target, click the Build Phases tab, and then add the following dependencies in the Link Binary With Libraries section:

      libz.tbd
      libc++.tbd
      deviceiOS.framework
    Add dependencies

Operation definition

The following section describes the response from Device Risk Detection when you use Device Risk Detection SDK for iOS:

/**
 * The deviceToken.
 */
@interface SecuritySession: NSObject
/**
 * The call status code of the operation.
 */
@property(atomic) int code;
/**
 * The session string.
 */
@property(copy, atomic) NSString * session;
@end
@interface SecurityDevice : NSObject
/**
 * The function used to initialize Device Risk Detection SDK for iOS.
 */
- (void)initDevice:(NSString *)userAppKey :(void (^)(int))initCallback;// The appkey. You can create an appkey in the  
Fraud Detection console 
.
/**
 * Obtain the deviceToken.
 */
- (SecuritySession *) getSession;
@end

Procedure

  1. Initialize Device Risk Detection SDK for iOS.

    The following sample code is applicable to Xcode 11 or earlier:

    // Initialize Device Risk Detection SDK for iOS.
    SecurityDevice * securityDevice = [
        [SecurityDevice alloc] init
    ];
    if (nil ! = securityDevice) {
        [securityDevice initDevice: @ "xxxxxx": ^ (int code) {
            NSString * initResult = [NSString stringWithFormat: @ "The initialization result of Device Risk Detection SDK for iOS %d", code];
            NSLog(@ "%@", initResult);
            if (10000 ! = code) {
                NSLog(@ "The initialization failed. The session string obtained by calling the getSession operation cannot be used to query risk tags.");
            } else {
                NSLog(@ "The initialization is successful. The getSession operation can be called as expected.");
            }
        }];
    }

    The following sample code is applicable to Xcode 12 or earlier:

    /**
     * For iOS 14, attempt to display a message to ask for user consent to use the Identifier for Advertisers (IDFA).
     */
    typedef void (^IDFARequestBlock)(bool success);
    API_AVAILABLE(ios(14))
    static bool isATTrackingEnabled(ATTrackingManagerAuthorizationStatus status){
        if(ATTrackingManagerAuthorizationStatusAuthorized == status){
            return true;
        }
        return false;
    }
    - (void)helperRequestIDFAPermissionWithBlock:(IDFARequestBlock) complete{
        if(@available(iOS 14, *)){
            ATTrackingManagerAuthorizationStatus authStatus = ATTrackingManager.trackingAuthorizationStatus;
            // No message appears.
            if(ATTrackingManagerAuthorizationStatusNotDetermined == authStatus){
                [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                    if(nil ! = complete){
                        return complete(isATTrackingEnabled(status));
                    }
                }];
            }else if(nil ! = complete){
                return complete(isATTrackingEnabled(authStatus));
            }
        }
    }
    - (void)initSecurityDevice{
        [securityDevice initDevice: @ "xxxxxx": ^ (int code) {
            NSString * initResult = [NSString stringWithFormat: @ "The initialization result of Device Risk Detection SDK for iOS %d", code];
            NSLog(@ "%@", initResult);
            if (10000 ! = code) {
                NSLog(@ "The initialization failed. The session string obtained by calling the getSession operation cannot be used to query risk tags.");
            } else {
                NSLog(@ "The initialization is successful. The getSession operation can be called as expected.");
            }
        }];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Note: You must distinguish iOS 14 from the other iOS operating systems if you start Device Risk Detection Fraud Detection in this way.
        if(@available(iOS 14, *)){
            [self helperRequestIDFAPermissionWithBlock:^(bool success){
                if(success){
                    NSLog(@"The permission to use the IDFA is obtained.");
                }else{
                    NSLog(@"The permission to use the IDFA is not obtained.");
                }
                [self initSecurityDevice];
            }];
        }else{
            [self initSecurityDevice];
        }
    }

  2. Obtain the deviceToken.

    Obtain the deviceToken by using Device Risk Detection SDK for iOS. You can call the getSession operation to obtain the session, and use the session to query the risk detection result of the current device from the server. The following sample code demonstrates how to call the getSession operation:

    // Obtain the session. You must call the getSession operation after you receive a callback notification indicating that Device Risk Detection SDK for iOS is initialized. Generally, the initialization is complete within 3 seconds.
    SecuritySession * securitySession = [securityDevice getSession];
    NSString * rs = [NSString stringWithFormat: @ "[%d]%@", securitySession.code, securitySession.session];
    NSLog(@ "Session=>%@", rs);
    if (10000 ! = code) {
        NSLog(@ "The session failed to be obtained. The call result cannot be used to query risk tags.");
    } else {
        NSLog(@ "The session is obtained and can be used to query risk tags.");
    }

What to do next

We recommend that you perform the following operations after you integrate Device Risk Detection SDK for iOS:

  • Before you publish an app to App Store, make sure that the app has applied for IDFA permission and the NSUserTrackingUsageDescription key is added to the Info.plist file. Otherwise, the app fails to be published to App Store. For more information, see NSUserTrackingUsageDescription.

  • Add the description of Privacy - Local Network Usage Description to the Info.plist file to tell users that the app checks the connectivity with the on-premises network. You can customize the description based on your requirements.

  • Make sure that the initialization is successful before you call the getSession operation to obtain the session. The initialization is successful if the code is 10000 in the callback notification that is received after the initDevice operation is called.

  • To prevent network connection failures when you call the initDevice operation, we recommend that you check whether the call to the initDevice operation is successful before you call the getSession operation. This ensures that the initialization is successful at least once when new initialization requests are initiated upon failed calls to the initDevice operation.

Call the Device Risk Detection API

Use the deviceToken and other parameters to call the Fraud Detection API in accordance with the following topics:

Device Risk Detection event and response parameters

The following figure shows the sequence diagram for integrating and using the SDK.

Note

Steps 1 and 2 are required only when you load the SDK for the first time. You can perform Steps 3 through 9 in a loop as needed.

Flow Chart of Device SDK