This topic describes the integration process for the Device Risk SDK for iOS.
Usage notes
The iOS SDK has the following limits:
-
iOS 9.0 or later is required.
Prerequisites
-
To fulfill the privacy compliance obligations that apply when you integrate a third-party SDK and to reduce privacy violation risks, use the latest version published on the Alibaba Cloud documentation center. Before you use Device Fraud Detection, read the personal information processing rules and the Fraud Detection SDK Privacy Policy, and integrate the SDK as described in SDK compliance guidelines.
Permissions
To improve risk detection accuracy, add the following fields and their usage descriptions to the Info.plist file of your app before you publish the app to the App Store. Failure to declare required fields may cause your app to fail App Store review.
|
Permission |
Required |
Description |
|
NSLocalNetworkUsageDescription |
No (recommended) |
Obtains device connectivity within the local area network to detect risks such as device farms and group control. |
|
NSLocationWhenInUseUsageDescription |
No (recommended) |
Obtains device location information to detect risks such as fake location spoofing. |
|
NSUserTrackingUsageDescription |
No (recommended) |
Obtains IDFA information to improve device fingerprint stability. |
Dependency configuration
-
Download the iOS SDK. The SDK is a standard static framework for Xcode. Generate an AppKey in the console from which you download the SDK.
-
The single-architecture framework file is approximately 2.5 MB.
-
To ensure anti-reverse engineering capabilities and data security during network transmission, the SDK includes extensive code obfuscation, encryption, and decryption operations. As a result, the SDK is relatively large.
-
-
Copy
deviceiOS.frameworkfrom the SDK package to your iOS project directory. -
In the project settings, choose Build Phases -> Link Binary With Libraries, and add
deviceiOS.frameworkand its dependencies:deviceiOS.framework CoreTelephony.framework CoreLocation.framework Security.framework libresolv.tbd libz.tbd libc++.tbd // Add the following frameworks if you use the IDFA version AppTrackingTransparency.framework AdSupport.framework -
Based on your business requirements, choose whether to use an SDK version that collects sensitive data such as
IDFA. For more information, see the description in the SDK download list.
Call the SDK
After completing the above configuration, follow these three steps to complete client integration:
-
Initialize (initDevice)
-
Obtain the client token (getDeviceToken)
-
Send the token to your business server
1. Initialize (initDevice)
This function initializes the SDK and collects information. Call this function as early as possible in risk detection scenarios, provided that compliance requirements are met. You need to call this function only once per app launch.
-
Function prototype
@interface SecurityDevice : NSObject
/**
* Initialize the device fingerprint SDK.
*/
- (void)initDevice:(NSString *)userAppKey :(void (^)(int))initCallback;
/**
* Initialize the SDK with options.
*/
- (void)initDevice:(NSString *)userAppKey withOptions:(NSMutableDictionary *)options callback:(void (^)(int))initCallback;
@end
Parameters
-
userAppKey: identifies your application. You can apply for the AppKey in Device App Management in the Alibaba Cloud Management Console. -
initCallback: the initialization callback listener. You can use the callback to check whether initialization succeeds. Default value:nil. For the valid values of thecodefield, see Status return values. -
options: the optional information collection parameters. Default value:nil. The following table describes the available options.
|
Parameter |
Description |
Example |
|
IPv6 |
Specifies whether to use an IPv6 domain name to report device information. 0 (default): uses an IPv4 domain name. 1: uses an IPv6 domain name. |
"0" |
|
CustomUrl |
The domain name of the server to which data is reported. Specify this parameter only when you report data to a specific site. You do not need to configure this parameter by default. |
"https://cloudauth-device.aliyuncs.com" |
|
CustomHost |
The host of the server to which data is reported. This parameter must be used together with CustomUrl. By default, you do not need to configure either parameter. |
"cloudauth-device.aliyuncs.com" |
|
DataType |
The type of device data that is not collected. This parameter is empty by default (recommended), which indicates that all data is collected. The following table describes the configurable data types. |
COLLECT_NO_EXTRA_DEVICE_DATA |
DataType parameter
|
Data type |
Description |
Device information fields |
|
COLLECT_NO_EXTRA_DEVICE_DATA |
Device extension information |
Includes the list of apps used for fraudulent activities, local area network IP address, DNS IP address, information about the connected Wi-Fi network (SSID and BSSID), and location information. |
CustomUrl and CustomHost parameters
To report data to a specific site, set CustomUrl and CustomHost to a specific region. You do not need to configure these parameters by default.
|
Region |
Endpoint |
|
Singapore (default) |
CustomUrl: https://cloudauth-device.ap-southeast-1.aliyuncs.com CustomHost: cloudauth-device.ap-southeast-1.aliyuncs.com |
|
China (Hong Kong) |
CustomUrl: https://cloudauth-device.cn-hongkong.aliyuncs.com CustomHost: cloudauth-device.cn-hongkong.aliyuncs.com |
|
Germany (Frankfurt) |
CustomUrl: https://cloudauth-device.eu-central-1.aliyuncs.com CustomHost: cloudauth-device.eu-central-1.aliyuncs.com |
|
US (Silicon Valley) |
CustomUrl: https://cloudauth-device.us-west-1.aliyuncs.com CustomHost: cloudauth-device.us-west-1.aliyuncs.com |
-
Example
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setValue:@"0" forKey:@"IPv6"]; // Use IPv4
// Specify a custom region to which data is reported
// [options setValue:@"xxx" forKey:@"CustomUrl"]; Set the reporting URL
// [options setValue:@"xxx" forKey:@"CustomHost"]; Set the reporting host
// Standard call (recommended)
[[SecurityDevice sharedInstance] initDevice:@"<your AppKey>" withOptions:options callback:nil];
// Call with a callback
[[SecurityDevice sharedInstance] initDevice:@"<your AppKey>" withOptions:options callback:^(int code) {
NSString * initResult = [NSString stringWithFormat: @"Initialization result: %d", code];
NSLog(@"%@", initResult);
if (SC_SUCCESS != code) {
NSLog(@"Initialization failed");
} else {
NSLog(@"Initialization succeeded");
}
}];
2. Obtain the client token (getDeviceToken)
Obtain the client token and report it to your business server. The server then calls the fraud detection API to query device risk information.
-
Make sure that the interval between the
initDevicecall and thegetDeviceTokencall is at least 3 seconds, or callgetDeviceTokenin the callback that indicates successful initialization. -
(Recommended) Specify the
bizIdparameter when you callgetDeviceToken. This binds the token to a unique business ID. Pass the same ID when you query the result on the server to verify that the token has not been tampered with. -
(Recommended) Call
getDeviceTokenon a non-main thread of the app to avoid crashes that a long-running call may cause. -
Function prototype
@interface SecurityDevice : NSObject
- (SecurityToken *)getDeviceToken;
- (SecurityToken *)getDeviceToken:(NSString *)bizId;
@end
The length of the token string depends on the network conditions. Under good network conditions, the token string is approximately 600 bytes. Under poor network conditions, the token string is approximately 2.5 KB.
If a large number of long tokens appear in your business:
First, make sure that the client network is stable.
Second, make sure that the interval between the initDevice call and the getDeviceToken call is at least 3 seconds, or call getDeviceToken in the callback that indicates successful initialization.
-
Example
// Specify bizId to prevent deviceToken from being tampered with. Replace the example value with your business ID.
NSString *bizId = @"1234567890abcdef******";
SecurityToken *deviceToken = [[SecurityDevice sharedInstance] getDeviceToken:bizId];
if (deviceToken == nil || SC_SUCCESS != deviceToken.code) {
NSLog(@"Failed to obtain the token. Code: %d", deviceToken.code);
} else {
NSLog(@"The token is obtained. You can query risk tags. Token: %@", deviceToken.token);
}
If you use IDFA or location information, the Apple privacy policy requires that, in addition to declaring the corresponding permissions in the Info.plist file of your app, you prompt the user for authorization in a dialog box. The development environment must use Xcode 12 or later.
Obtain the client token in business scenarios that require risk detection, such as registration and promotional campaigns, and report the token to your business server for risk queries.
Pass deviceToken together with other parameters to the fraud detection API as described in Device Fraud Detection event parameters.
3. Send the token to your business server
After you obtain the deviceToken, pass it as a parameter to your business server. The server calls the Alibaba Cloud Device Fraud Detection API with the deviceToken to query and verify device risk information.
Status return values
|
SecurityCode |
Code |
Description |
|
SC_SUCCESS |
10000 |
The SDK is initialized. |
|
SC_NOT_INIT |
10001 |
The SDK is not initialized. |
|
SC_NOT_PERMISSION |
10002 |
The basic permissions that the SDK requires are not fully granted. |
|
SC_UNKNOWN_ERROR |
10003 |
An unknown system error occurred. |
|
SC_NETWORK_ERROR |
10004 |
A network error occurred. |
|
SC_NETWORK_ERROR_EMPTY |
10005 |
A network error occurred. The returned content is an empty string. |
|
SC_NETWORK_ERROR_INVALID |
10006 |
The format of the network response is invalid. |
|
SC_PARSE_SRV_CFG_ERROR |
10007 |
Failed to parse the server configuration. |
|
SC_NETWORK_RET_CODE_ERROR |
10008 |
The gateway returned a failure response. |
|
SC_APPKEY_EMPTY |
10009 |
The AppKey is empty. |
|
SC_PARAMS_ERROR |
10010 |
Another parameter error occurred. |
|
SC_FGKEY_ERROR |
10011 |
A key calculation error occurred. |
|
SC_APPKEY_ERROR |
10012 |
The AppKey is invalid. |
Complete code example
static NSString *USER_APP_KEY = @"<Obtain the AppKey in the console>";
- (void)viewDidLoad {
[super viewDidLoad];
// Integration example
[self doStandard];
}
- (void)doStandard {
// Initialize the SDK
// You need to call this method only once in the app lifecycle
[self doInit];
dispatch_async(dispatch_get_global_queue(NULL, NULL), ^{
// This delay is for demonstration only. No sleep is needed in production.
[NSThread sleepForTimeInterval:2.0];
// Obtain the token
[self doGetToken];
});
}
- (void)doInit {
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setValue:@"0" forKey:@"IPv6"]; // Use IPv4
// Specify a custom region to which data is reported
// [options setValue:@"https://cloudauth-device.aliyuncs.com" forKey:@"CustomUrl"];
// [options setValue:@"cloudauth-device.aliyuncs.com" forKey:@"CustomHost"];
[[SecurityDevice sharedInstance] initDevice:USER_APP_KEY withOptions:options callback:nil];
}
- (void)doGetToken {
// Specify bizId to prevent deviceToken from being tampered with. Replace the example value with your business ID.
NSString *bizId = @"1234567890abcdef******";
SecurityToken *deviceToken = [[SecurityDevice sharedInstance] getDeviceToken:bizId];
if (deviceToken == nil || SC_SUCCESS != deviceToken.code) {
NSLog(@"Failed to obtain the token. The result cannot be used to query risk tags.");
} else {
NSLog(@"The token is obtained. You can query risk tags. Token: %@", deviceToken.token);
}
}
FAQ
For frequently asked questions about Device Fraud Detection SDK integration, see FAQ about Device Fraud Detection SDK integration.