This topic describes how to integrate CAPTCHA into an iOS client and provides API usage examples.
Procedure
Download the SDK
Make sure to download the correct SDK:
On the Download API & SDK page, configure the following settings: For Step 1 Select Product to Integrate, select Captcha. For Step 2 Select Client Type, select App. For Step 3 Select Platform, select iOS. For Step 4 Select Version, select Standard. Then, click Download SDK (with demo).
Log on to the Phone Number Verification Service console. On the Overview page, find the API&SDK area on the right and click Download Now. On the API&SDK page, download and decompress the SDK as instructed.
Create a verification plan
To get the plan code and other parameters required for project imports or API calls, navigate to Phone Number Verification Service in the Phone Number Verification Service console.
Import the SDK
-
If you add the SDK manually, drag the downloaded AlicomCaptcha4.framework file into your project and make sure the Copy items if needed checkbox is selected. Import the framework in Linked Frameworks and Libraries. Verify that the .framework file is added to .
-
To load Objective-C Category files from the static library, add -ObjC to for the corresponding target.
-
Import AlicomCaptcha4.bundle into your project by dragging the AlicomCaptcha4.bundle file from the SDK path. This bundle is required for verification.
API usage
-
Initialize the verification with a CAPTCHA ID.
-
Start the verification.
-
Obtain the verification result parameters and use them to perform secondary verification to prevent forgery.
-
Use the error delegate method to handle errors during verification.
You must conform to the <AlicomCaptcha4SessionTaskDelegate> delegate protocol to handle verification results and errors.
Code examples
Import the AlicomCaptcha4.framework static library into your project.
#import <AlicomCaptcha4/AlicomCaptcha4.h>
Initialize
Initialize an instance of the AlicomCaptcha4Session verification manager and set its delegate. You can initialize the AlicomCaptcha4Session instance in advance or let it be created on-demand through lazy loading. For other optional configurations, refer to the APIs and properties defined in AlicomCaptcha4SessionConfiguration.
#define captchaID @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
@interface ViewController () <AlicomCaptcha4SessionTaskDelegate>
@property (strong, nonatomic) IBOutlet UIButton *startBtn;
@property (nonatomic, strong) AlicomCaptcha4Session *captchaSession
@end
@implementation ViewController
- (AlicomCaptcha4Session *)captchaSession {
if (!_captchaSession) {
_captchaSession = [AlicomCaptcha4Session sessionWithCaptchaID
/// To modify the default configuration,
/// you can create an instance by using the commented-out code below.
// AlicomCaptcha4SessionConfiguration *config = [AlicomCaptcha4SessionConfiguration defaultConfiguration];
// config.timeout = 8.0f;
// ...
// _captchaSession = [AlicomCaptcha4Session sessionWithCaptchaID:captchaID configuration:config];
_captchaSession.delegate = self;
}
return _captchaSession;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize the verification session in advance. If you do not call it here, the lazy loading mode is used.
[self captchaSession];
[self.startBtn addTarget:self action:@selector(start) forControlEvents
}
Start verification
After initialization is complete, call the following method to start the verification.
- (void)start {
[self.captchaSession verify];
}
Handle verification errors
To handle verification errors, implement the following method from the AlicomCaptcha4SessionTaskDelegate delegate protocol. For more details about errors, see Error codes.
- (void)alicomCaptchaSession:(AlicomCaptcha4Session *)captchaSession didReceiveError
NSLog(@"error: %@", error.description);
}