产品动态
产品公告

// 请在任何调用腾讯验证码服务的逻辑之前设置Captcha App IDTencentCaptcha.setup { config in// 请使用您的验证码App ID替换__YOUR_CAPTCHA_APP_ID__config.appID=__YOUR_CAPTCHA_APP_ID__}
appID | 说明 |
appID | 请使用我们分配给您的 AppID 填入到该参数 |
tarsServerURL | Tars 服务器地址 |
captchaPageHost | 设置验证码页面的 Host |
usesCacheToken | 是否允许使用缓存的 token 以加速请求过程 |
miniTuringShieldChannel | 风险识别业务的渠道号,请填入我们分配给您的渠道ID |
timeoutForNativeRequest | 【停用,请统一使用timeout】本地设备风险检测的请求超时时长,单位秒 |
timeoutForNativePacking | 【停用,请统一使用timeout】本地设备风险检测的采集超时时长,单位秒 |
timeoutForWebViewLoading | 【停用,请统一使用timeout】页面加载的超时时长,单位秒 |
timeout | 统一超时设置接口,单位秒 |
override funcviewDidLoad(){super.viewDidLoad()// Do any additional setup after loading the view.letcontentController=WKUserContentController()letconfig=WKWebViewConfiguration()config.userContentController=contentControllerwebView=WKWebView(frame: self.view.bounds, configuration: config)webView.navigationDelegate=selfwebView.autoresizingMask=[.flexibleWidth, .flexibleHeight]self.view.addSubview(webView)}
TencentCaptcha.userDidAcceptPrivacyPolicy = true
let options = TencentCaptchaOptions()options.optionsCallback = { functionName, data in }// TODO: 根据需要设置其它选项TencentCaptcha.shared().requestCaptcha(withWebView: self.webView, options:options) { error, result inif let error = error {// 如果发生错误,则error为非空}else {// result为返回值}}
let options = TencentCaptchaOptions()options.loadingEnabled = TencentCaptchaOptionsSwitchOnoptions.setExtractParameter(true, forName: "enableAutoCheck")TencentCaptcha.shared().requestCaptcha(withWebView: self.webView, options:options) { error, result in// ...}
配置名 | 值类型 | 说明 |
enableAutoCheck | Bool | 开启后,对安全的请求可自动完成勾选验证,无需用户操作。 true:开启 false:关闭 nil:默认(关闭) |
/// 深夜模式选项typedef enum : NSUInteger {/// 默认TencentCaptchaOptionsDarkModeDefault,/// 随系统开启TencentCaptchaOptionsDarkModeTrue,/// 总是关闭TencentCaptchaOptionsDarkModeFalse,/// 强制开启TencentCaptchaOptionsDarkModeForce,} TencentCaptchaOptionsDarkMode;typedef enum : NSUInteger {TencentCaptchaOptionsSwitchDefault,TencentCaptchaOptionsSwitchOn,TencentCaptchaOptionsSwitchOff,} TencentCaptchaOptionsSwitch;/// 验证码请求参数@interface TencentCaptchaOptions : NSObject/// 自定义透传参数,业务可用该字段传递少量数据,该字段的内容会被带入 callback 回调的对象中。@property (nonatomic, strong, nullable) NSString *bizState;/// 深夜模式@property (nonatomic, assign) TencentCaptchaOptionsDarkMode darkModeEnabled;/// 帮助按钮开关@property (nonatomic, assign) TencentCaptchaOptionsSwitch needsFeedback;/// 自定义帮助按钮的链接/// @discussion 如`customFeedbackURL`非空,则`needsFeedback`选项无效,帮助按钮总是显示,且点击时跳转到该URL@property (nonatomic, strong, nullable) NSString *customFeedbackURL;/// 指定验证码提示文案的语言,优先级高于控制台配置@property (nonatomic, strong, nullable) NSString *userLanguage;/// 适老化模式@property (nonatomic, assign) TencentCaptchaOptionsSwitch agedEnabled;/// 定义验证码展示方式/// @discussion popup(默认)弹出式,以浮层形式居中弹出展示验证码。@property (nonatomic, strong, nullable) NSString *type;/// 是否在验证码加载过程中显示loading框@property (nonatomic, assign) TencentCaptchaOptionsSwitch loadingEnabled;/// CaptchaAppId 加密校验串/// @discussion 详见 https://www.tencentcloud.com/zh/document/product/1159/49680?lang=zh&pg=#captchaAppid@property (nonatomic, copy, nullable) NSString *encryptedAppID;/// 设置额外的参数/// @param value 参数的值/// @param name 参数的关键字名称/// @discussion 当name对应的参数不存在,会插入新值,已存在时,新值会覆盖旧值;当value为nil时,将删除该参数- (void)setExtractParameter:(nullable id)value forName:(nonnull NSString *)name;/// 当前已设置的额外参数@property (nonatomic, strong, readonly, nonnull) NSDictionary<NSString *, id> *extractParameters;@end
/// 深夜模式选项typedef enum:NSUInteger{/// 默认TencentCaptchaOptionsDarkModeDefault,/// 随系统开启TencentCaptchaOptionsDarkModeTrue,/// 总是关闭TencentCaptchaOptionsDarkModeFalse,/// 强制开启TencentCaptchaOptionsDarkModeForce,}TencentCaptchaOptionsDarkMode;
/// 开关选项typedef enum : NSUInteger {TencentCaptchaOptionsSwitchDefault,TencentCaptchaOptionsSwitchOn,TencentCaptchaOptionsSwitchOff,} TencentCaptchaOptionsSwitch;
/// 验证码服务配置/// - 用于在 `+[TencentCaptcha setupWithConfigurationHandler:]` 中配置服务/// - SeeAlso:/// - Objective C: `+[TencentCaptcha setupWithConfigurationHandler:]`,/// - Swift: `TencentCaptcha.setup(configurationHandler:)`@interface TencentCaptchaConfiguration : NSObject- (nonnull instancetype)init NS_UNAVAILABLE;@property (nonnull, nonatomic, copy) NSString *appID;/// Tars服务器地址/// - Note: 如果不清楚用途,请不要设置@property (nullable, nonatomic, copy) NSURL *tarsServerURL;/// 设置验证码页面的Host/// - Note:/// - 只能设置URL中的host部分,例如, `https://host/path/to/html` 中的host部分,其它部分目前不支持修改/// - 如果host不合法,导致无法生成正确的页面URL,则该设置无效/// - 设置为nil可以使其保持为默认值/// - 如果不清楚用途,请不要设置@property (nullable, nonatomic, copy) NSString *captchaPageHost;/// 是否允许使用缓存的token以加速请求过程/// - Note: 默认为YES,缓存策略能满足绝大多数场景的需求。如果您特别关注实时风险,请设置为NO@property (nonatomic, assign) BOOL usesCacheToken;/// 【停用,请统一使用timeout】/// 本地设备风险检测的请求超时时长,单位秒@property (nonatomic, assign) NSTimeInterval timeoutForNativeRequest__attribute__((deprecated("NOT WORK! Use 'timeout' instead.")));/// 【停用,请统一使用timeout】/// 本地设备风险检测的采集超时时长,单位秒@property (nonatomic, assign) NSTimeInterval timeoutForNativePacking__attribute__((deprecated("NOT WORK! Use 'timeout' instead.")));/// 【停用,请统一使用timeout】/// 页面加载的超时时长,单位秒@property (nonatomic, assign) NSTimeInterval timeoutForWebViewLoading__attribute__((deprecated("NOT WORK! Use 'timeout' instead.")));/// 统一超时设置,单位秒,内部会进行各个阶段的超时控制分配,简化超时设置逻辑@property (nonatomic, assign) NSTimeInterval timeout;@end
/// 验证码服务的实现类@interface TencentCaptcha:NSObject-(nonnull instancetype)init NS_UNAVAILABLE;/// 验证码服务的单例+(nonnull instancetype)sharedService NS_SWIFT_NAME(shared());/// 配置服务选项/// - Parameter configurationHandler: 如需修改配置,请修改此回调函数中提供的configuration中的属性-(void)setupWithConfigurationHandler:(nonnull void(^)(TencentCaptchaConfiguration *_Nonnull))configurationHandler NS_SWIFT_NAME(setup(configurationHandler:));/// 请求验证码/// - Parameters:/// - webView: 用于展示验证码的网页视图/// - options: 验证码请求参数/// - completionHandler: 验证结束的回调////// @discussion 由于某些系统限制,SDK暂时无法接收来自StoryBoard生成的WKWebView。-(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:));/// 设置额外的参数/// @param value 参数的值/// @param name 参数的关键字名称/// @discussion 当name对应的参数不存在,会插入新值,已存在时,新值会覆盖旧值;当value为nil时,将删除该参数- (void)setExtractParameter:(nullable id)value forName:(nonnull NSString *)name;/// 当前已设置的额外参数@property (nonatomic, strong, readonly, nonnull) NSDictionary<NSString *, id> *extractParameters;@end
/// 验证码服务的实现类/// - 提供主要的验证码服务能力/// - Example:/// ```swift/// // Step 1. 引导用户同意用户隐私协议,然后设置`userDidAcceptPrivacyPolicy`/// TencentCaptcha.userDidAcceptPrivacyPolicy = true/// // Step 2. 初始化SDK/// TencentCaptcha.setup(...)/// // Step 3. 生成WebView并配置/// webView = WKWebView(...)/// self.view.addSubview(webView)/// // Step 4. 生成和展示验证码/// TencentCaptcha.requestCaptcha(...)///@interface TencentCaptcha : NSObject- (nonnull instancetype)init NS_UNAVAILABLE;/// 验证码服务的单例+ (nonnull instancetype)sharedService NS_SWIFT_NAME(shared());/// 用户是否已同意隐私政策/// 调用任何SDK功能前,请先引导用户同意隐私协议,然后先将此类属性设为 `YES` / `true`/// - Example:/// ```swift/// // 用户同意隐私协议后/// TencentCaptcha.userDidAcceptPrivacyPolicy = true/// ```/// - Warning: 用户未同意隐私协议前,SDK不提供验证码服务@property (nonatomic, assign, class) BOOL userDidAcceptPrivacyPolicy;@property (nonatomic, readonly, nonnull, class) TencentCaptchaConfiguration *configuration;/// 配置服务选项/// - Parameters:/// - configurationHandler: 如需修改配置,请修改此回调函数中提供的configuration中的属性////// - Example: 最小化设置/// ```/// TencentCaptcha.setup { config in/// config.appID = __YOUR_CAPTCHA_APP_ID__ // 请使用您的验证码App ID替换__YOUR_CAPTCHA_APP_ID__/// }/// ```/// - Note: 请在尽可能靠前的时机配置服务,并确保在使用服务前配置/// - SeeAlso: `TencentCaptchaConfiguration`+ (void)setupWithConfigurationHandler:(nonnull void(^)(TencentCaptchaConfiguration *_Nonnull))configurationHandler NS_SWIFT_NAME(setup(configurationHandler:));/// 请求验证码/// - Parameters:/// - webView: 用于展示验证码的网页视图,需要由调用方初始化/// - options: 验证码请求参数/// - completionHandler: 验证结束的回调////// - Example:/// ```swift/// TencentCaptcha.shared().requestCaptcha(withWebView: webView, options: options) { error, result in/// if let result = result {/// // 当token不为空时,验证码验证成功/// }/// else {/// // 否则error会输出错误,具体错误域和错误号请参见文档/// }/// }/// ```////// - Warning: 由于某些系统限制,SDK暂时无法接收来自StoryBoard生成的WKWebView。请使用代码方式初始化一个WKWeView并传递给SDK,例如:/// ```swfit/// let contentController = WKUserContentController()/// let config = WKWebViewConfiguration()/// config.userContentController = contentController/// webView = WKWebView(frame: self.view.bounds, configuration: config)/// webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]/// self.view.addSubview(webView)/// ```- (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
字段名 | 值类型 | 说明 |
ret | Int | 验证结果,0:验证成功。2:用户主动关闭验证码。 |
ticket | String | 验证码返回的票据,当且仅当 ret = 0 时 ticket 有值。 |
CaptchaAppId | String | 验证码应用 ID。 |
bizState | Any | 自定义透传参数。 |
randstr | String | 本次验证的随机串,后续票据校验时需传递该参数。 |
errorCode | Number | |
errorMessage | String | 错误信息。 |
错误代码 | 说明 |
0 | 没有错误 |
1 | 请求协议打包错误 |
2 | 回复协议解包错误 |
3 | 空回复错误 |
31 | X1协议数据压缩错误 |
4 | Shark协议shark层解包错误 |
5 | Shark协议shark层未包含sashimi |
6 | Shark协议sashimi层解包错误 |
7 | Shark协议sashimi层空包错误 |
8 | Shark协议注册GUID错误 |
9 | Shark协议保存GUID错误 |
10 | Shark协议命令字无法识别 |
20 | 会话过期 |
21 | 鲨鱼皮层的异常返回 |
22 | Shark Skin层解包错误 |
23 | Shark协议sashimi层返回值非0 |
11 | 记录为空 |
12 | 记录内容不合法或部分数据缺失(DEBUG模式有错误详情) |
13 | 发送Shark请求时未带GUID(目前仅逻辑上理论存在) |
14 | 发送Shark/WUP请求时未包含记录 |
15 | 当前GUID未正确对应服务器类型 |
16 | 生成回复内容时既没有包含错误也没有包含服务器回复(目前仅逻辑上理论存在) |
17 | HTTP请求的客户端错误(status code在400~499之间) |
18 | HTTP请求的服务端错误(status code在500~599之间) |
19 | 收到的回复与请求不对应 |
25 | 暂时无法请求 |
26 | 非法内部调用 |
27 | 请求超时 |
28 | 非法通讯协议,或者未正确选择通讯协议 |
29 | 请求队列过长 |
30 | 用户/业务端取消请求 |
24 | 内部测试保留 |
34 | 生成token时序列化失败 |
35 | 用户未同意隐私协议 |
38 | 未正确设置渠道号 |
37 | 不可用的方法或函数 |
36 | 异常捕捉(详细异常情况会在 error.userInfo 中提供) |
文档反馈