Integrate the iOS SDK
This topic describes how to integrate the Anti-Bot SDK into your iOS application.
iOS SDK files
Contact Alibaba Cloud technical support to obtain and decompress the SDK package. The decompressed sdk-iOS folder contains the following files:
|
File name |
Description |
|
SGMain.framework |
Main framework. |
|
SecurityGuardSDK.framework |
Basic security plug-in. |
|
SGSecurityBody.framework |
Bot recognition plug-in. |
|
SGAVMP.framework |
Virtual machine engine plug-in. |
|
yw_1222_0335_mwua.jpg |
Configuration file. |
Configure the iOS project
-
In your project, go to the Build Phases tab, select Link Binary With Libraries, and add the following four .framework dependency files from the SDK package:
-
SGMain.framework
-
SecurityGuardSDK.framework
-
SGSecurityBody.framework
-
SGAVMP.framework
-
-
On the Build Settings tab, set Linking > Other Linker Flags to -ObjC.
-
Add the following system dependency files to your project's dependency library:
-
CoreFoundation.framework
-
CoreLocation.framework
-
AdSupport.framework
-
CoreTelephony.framework
-
CoreMotion.framework
-
SystemConfiguration.framework
You must also add
libz.1.2.8.tbd. -
-
Add the yw_1222_0335_mwua.jpg configuration file from the SDK package to the mainbundle directory.
ImportantIf your application integrates multiple targets, add the
yw_1222_0335_mwua.jpgconfiguration file to the correct Target Membership.
Call the SDK API
Step 1: Initialize the SDK
API definition: + (BOOL) initialize;
Description: Initializes the SDK.
Parameters: None.
Return value: A Boolean value. Returns YES on success or NO on failure.
Example call: [JAQAVMPSignature initialize];
Example code
static BOOL avmpInit = NO;
- (BOOL) initAVMP{
@synchronized(self) { // just initialize once
if(avmpInit == YES){
return YES;
}
avmpInit = [JAQAVMPSignature initialize];
return avmpInit;
}
}
Step 2: Sign request data
API definition: + (NSData*) avmpSign: (NSInteger) signType input: (NSData*) input;
Description: Signs the input data with the AVMP technique and returns a signature string.
The signed request body must be identical to the one sent by the client, including its encoding, spaces, special characters, and parameter order. Any discrepancy causes the signature verification to fail.
Parameters
|
Parameter |
Type |
Required |
Description |
|
signType |
NSInteger |
Yes |
The signing algorithm. Set this parameter to the fixed value |
|
input |
NSData* |
No |
The data to be signed, typically the entire request body. Note
If the request body is empty, for example, in a GET request or a POST request with an empty body, pass |
Return value: An NSData* object that contains the signature string.
Example call: [JAQAVMPSignature avmpSign: 3 input: request_body];
Example code
Before sending data to the server, call the avmpSign API to sign the entire request body. The returned signature string is the wToken.
# define VMP_SIGN_WITH_GENERAL_WUA2 (3)
- (NSString*) avmpSign{
@synchronized(self) {
NSString* request_body = @"The request body, which can be encrypted or plaintext.";
if(![self initAVMP]){
[self toast:@"Error: init failed"];
return nil;
}
NSString* wToken = nil;
NSData* data = [request_body dataUsingEncoding:NSUTF8StringEncoding];
NSData* sign = [JAQAVMPSignature avmpSign: VMP_SIGN_WITH_GENERAL_WUA2 input:data];
if(sign == nil || sign.length <= 0){
return nil;
}else{
wToken = [[NSString alloc] initWithData:sign encoding: NSUTF8StringEncoding];
return wToken;
}
}
}
If the request body is empty, you must still call the avmpSign API to generate a wToken. Pass nil as the second parameter. The following code provides an example:
NSData* sign = [JAQAVMPSignature avmpSign: VMP_SIGN_WITH_GENERAL_WUA2 input:nil];
Step 3: Add the wToken to the header
Example code
#define VMP_SIGN_WITH_GENERAL_WUA2 (3)
-(void)setHeader
{ NSString* request_body = @"The request body, which can be encrypted or plaintext.";
NSData* body_data = [request_body dataUsingEncoding:NSUTF8StringEncoding];
NSString* wToken = nil;
NSData* sign = [JAQAVMPSignature avmpSign: VMP_SIGN_WITH_GENERAL_WUA2 input:body_data];
wToken = [[NSString alloc] initWithData:sign encoding: NSUTF8StringEncoding];
NSString *strUrl = [NSString stringWithFormat:@"http://www.aliyundoc.com/login"];
NSURL *url = [NSURL URLWithString:strUrl];
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
[request setHTTPMethod:@"POST"];
// Set the request body.
[request setHTTPBody:body_data];
// Add the wToken to the header.
[request setValue:wToken forHTTPHeaderField:@"wToken"];
NSURLConnection *mConn = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:true];
[mConn start];
// ...
}
Step 4: Send the data to the server
Send the request with the modified header to Alibaba Cloud Security. Alibaba Cloud Security parses the wToken to identify risks and block malicious requests, then forwards legitimate requests to the origin server.
Error codes
If an exception occurs or signature generation fails, search the console log for SG Error to find the corresponding error code.
The following table lists common error codes.
|
Error code |
Description |
|
1901 |
Invalid parameters. |
|
1902 |
Image file error. Possible cause: A bundle ID mismatch. |
|
1903 |
The image file format is invalid. |
|
1904 |
The image file version is outdated. The AVMP signature feature supports only v5 images. Upgrade the image file. |
|
1905 |
Image file not found. Ensure the yw_1222_0335_mwua.jpg file is added to the project correctly. |
|
1906 |
The image file is missing the required bytecode for the AVMP signature. Check whether you are using the correct image file. |
|
1907 |
Failed to initialize AVMP. Please try again. |
|
1910 |
Invalid avmpInstance. Possible causes:
|
|
1911 |
The bytecode of the encrypted image does not have the corresponding exported function. |
|
1912 |
AVMP call failed. Contact Alibaba Cloud technical support. |
|
1913 |
InvokeAVMP was called after the AVMPInstance was destroyed. |
|
1915 |
Insufficient memory for the AVMP call. Try again. |
|
1999 |
An unknown error occurred. Try again. |