// Set the Captcha App ID before any logic that calls Tencent Captcha
service
TencentCaptcha.shared
(
)
.setup
{
config
in
// Replace __YOUR_CAPTCHA_APP_ID__ with your Captcha App ID
config.appID
=
__YOUR_CAPTCHA_APP_ID__
}
override func
viewDidLoad
(
)
{
super.viewDidLoad
(
)
// Do any additional setup after loading the view.
let
contentController
=
WKUserContentController
(
)
let
config
=
WKWebViewConfiguration
(
)
config.userContentController
=
contentController
webView
=
WKWebView
(
frame: self.view.bounds, configuration: config
)
webView.navigationDelegate
=
self
webView.autoresizingMask
=
[
.flexibleWidth, .flexibleHeight
]
self.view.addSubview
(
webView
)
}
TencentCaptcha.shared
(
)
.requestCaptcha
(
withWebView: self.webView, options:options
)
{
error, result
in
if
let
error
=
error
{
// If an error occurs, error is non-null
}
else
{
// result is the
return
value
}
}
/// Captcha request parameters
@interface TencentCaptchaOptions
:
NSObject
/// Custom pass-through parameters. This field can be used by the business to pass a small amount of data, and its content will be included
in
the callback object.
@property
(
nonatomic, strong, nullable
)
NSString *bizState
;
/// Night mode
@property
(
nonatomic, assign
)
TencentCaptchaOptionsDarkMode darkModeEnabled
;
/// Help button switch
@property
(
nonatomic, assign
)
BOOL needsFeedback
;
/// Custom
help
button
link
/// @discussion If
`customFeedbackURL`
is not empty, the
`needsFeedback`
option is invalid, and the
help
button is always displayed, redirecting to the URL when clicked
@property
(
nonatomic, strong, nullable
)
NSString *customFeedbackURL
;
/// Specify the language
for
the Captcha prompt text,
which
takes precedence over console configuration
@property
(
nonatomic, strong, nullable
)
NSString *userLanguage
;
/// Elderly-friendly mode
@property
(
nonatomic, assign
)
BOOL agedEnabled
;
/// Define Captcha display method
/// @discussion
<
li
>
popup
(
default
)
- Pop-up style, displays the Captcha
in
a centered floating layer. embed - Embedded style, displays the Captcha embedded
in
the specified container element.
@property
(
nonatomic, strong, nullable
)
NSString *type
;
/// Whether to show a loading box during Captcha loading
@property
(
nonatomic, assign
)
BOOL loadingEnabled
;
/// CaptchaAppId encrypted validation string
/// @discussion For details, see https://www.tencentcloud.com/zh/document/product/1159/49680?lang
=
zh
&
pg
=
#captchaAppid
@property
(
nonatomic, assign
)
BOOL AppIDEncrypted
;
/// parameter and intermediate process callback
/// @discussion Set this callback when you need to receive intermediate parameters
@property
(
nonatomic, copy, nullable
)
void
(
^optionsCallback
)
(
NSString *_Nonnull functionName, NSDictionary
<
NSString *, id
>
*_Nonnull data
)
;
@end
/// Night mode options
typedef enum
:
NSUInteger
{
/// Default
TencentCaptchaOptionsDarkModeDefault,
/// Enabled with system
TencentCaptchaOptionsDarkModeTrue,
/// Always disabled
TencentCaptchaOptionsDarkModeFalse,
/// Force enabled
TencentCaptchaOptionsDarkModeForce,
}
TencentCaptchaOptionsDarkMode
;
/// Captcha
service
configuration
/// @discussion
/// Used to configure the
service
in
`+[TencentCaptcha TencentCaptcha:]`
@interface TencentCaptchaConfiguration
:
NSObject
-
(
nonnull instancetype
)
init NS_UNAVAILABLE
;
@property
(
nonnull, nonatomic, copy
)
NSString *appID
;
/// Tars server address
/// @discussion If you are unsure of its purpose,
do
not
set
it
@property
(
nullable, nonatomic, copy
)
NSURL *tarsServerURL
;
/// The URL of the Captcha HTML page
/// @discussion If you are unsure of its purpose,
do
not
set
it
@property
(
nullable, nonatomic, copy
)
NSURL *webPageURL
;
/// Whether to allow using cached tokens to speed up the request process
/// @discussion The default is YES, and the caching strategy can meet
most
scenarios. If you are particularly concerned about real-time risks,
set
it to NO
@property
(
nonatomic, assign
)
BOOL usesCacheToken
;
@end
/// The implementation class of the Captcha
service
@interface TencentCaptcha
:
NSObject
-
(
nonnull instancetype
)
init NS_UNAVAILABLE
;
/// The singleton of the Captcha
service
+
(
nonnull instancetype
)
sharedService NS_SWIFT_NAME
(
shared
(
))
;
/// Configuration
service
options
/// - Parameter configurationHandler: To modify the configuration, change the properties
in
the configuration provided
in
this callback
function
-
(
void
)
setupWithConfigurationHandler:
(
nonnull void
(
^
)
(
TencentCaptchaConfiguration *_Nonnull
))
configurationHandler NS_SWIFT_NAME
(
setup
(
configurationHandler:
))
;
/// Request Captcha
/// - Parameters:
/// - webView: The web view used to display the Captcha
/// - options: Captcha request parameters
/// - completionHandler: Completion callback
for
verification
///
/// @discussion Due to some system limitations, the SDK cannot currently receive WKWebView generated from StoryBoard.
-
(
void
)
requestCaptchaWithWebView:
(
nonnull WKWebView *
)
webView options:
(
nullable TencentCaptchaOptions *
)
options withCompletionHandler:
(
nonnull void
(
^
)
(
NSError *_Nullable error, NSDictionary *_Nullable result
))
completionHandler NS_SWIFT_NAME
(
requestCaptcha
(
withWebView:options:withCompletionHandler:
))
;
@end
Field Name | Value Type | Description |
ret | Int | Verification result, 0: Verification successful. |
ticket | String | Ticket returned by the Captcha, ticket has a value if and only if ret = 0. |
CaptchaAppId | String | Captcha Application ID. |
bizState | Any | Custom pass-through parameters. |
randstr | String | The random string for this verification, which needs to be passed during subsequent ticket verification. |
errorCode | Number | |
errorMessage | String | Error message. |
Error Code | Description |
0 | No error |
1 | Request protocol packaging error |
2 | Reply protocol unpacking error |
3 | Empty reply error |
31 | X1 protocol data compression error |
4 | Shark protocol shark layer unpacking error |
5 | Shark protocol shark layer does not contain sashimi |
6 | Shark protocol sashimi layer unpacking error |
7 | Shark protocol sashimi layer empty packet error |
8 | Shark protocol registration GUID error |
9 | Shark protocol save GUID error |
10 | Shark protocol unrecognized command word |
20 | Session expiration |
21 | Abnormal return of Shark Skin layer |
22 | Shark Skin layer unpacking error |
23 | Shark protocol sashimi layer return value is non-zero |
11 | The record is empty |
12 | The record content is invalid or some data is missing (error details available in DEBUG mode) |
13 | GUID not included when sending Shark request (currently only theoretically possible) |
14 | Record not included when sending Shark/WUP request |
15 | Current GUID does not correctly correspond to server type |
16 | Neither error nor server response included when generating reply content (currently only theoretically possible) |
17 | Client error for HTTP request (status code between 400~499) |
18 | Server error for HTTP request (status code between 500~599) |
19 | Received response does not correspond to the request |
25 | Temporarily unable to request |
26 | Illegal internal call |
27 | Request timeout |
28 | Illegal communication protocol, or incorrect communication protocol selected |
29 | Request queue too long |
30 | User/business side cancels the request |
24 | Internal testing reserved |
Was this page helpful?