You need to enable JavaScript to run this app.
导航

iOS版本

最近更新时间2024.01.10 15:52:11

首次发布时间2022.10.27 23:22:22

接入前体验

若无火山引擎销售人员与您对接,请点击此处申请试用,咨询问题请注明为身份认证。

IOS端体验地址

获取SDK包与Demo

IOS SDK

请先参照开通服务页流程获得身份认证服务授权,再下载SDK包进行使用。

BytedFaceVerifySdk.framework_v2.1.0.tar.gz
3.00MB

Demo

BytedCertObjcSample_v2.1.0.zip
3.05MB
在工程中导入SDK

1. 将BytedFaceVerifySdk.framework导入工程

在Xcode工具中点击TARGETS->Build Phases-> Link Binary With Libaries中点击“+”按钮,在弹出的窗口中点击“Add Other”按钮,选择BytedFaceVerifySdk.framework

完成上述步骤后,可以在Build Phases > Link Binary With Libraries看到导入的依赖包

2. 添加系统权限

因SDK需要使用相册的权限权限,请在 info.plist 中添加 :
Privacy - Camera Usage Description
Privacy - Photo Library Usage Description
Privacy - Microphone Usage Description

接口说明

1. 入口类BytedCertWrapper

1.1 获取BytedCertWrapper单例

函数原型:(instancetype)sharedInstance;
参数: void
返回: BytedCertWrapper单例对象
ex:

[BytedCertWrapper sharedInstance];

1.2 设置鉴权参数

函数原型:
- (void)setupSignWithStsToken:(NSString *)stsToken accessKey:(NSString *)accessKey secretAccessKey:(NSString *)secretAccessKey;

参数:

stsToken //使用临时密钥需要传入ststoken, 使用长期密钥可以直接传null
  accessKey //密钥ak
  secretAccessKey //密钥sk

返回:void
ex:

[BytedCertWrapper.sharedInstance setupSignWithStsToken:@"" accessKey:@"***" secretAccessKey:@"***"];

1.3 配置视频录制参数

函数名:(void)configRecordeAndUploadParams:(BytedCertTosInfo*)tosInfo callback:(BytedCertUploadVideoFinishBlock) callback
参数:
BytedCertTosInfo tosInfo //tos配置
BytedCertUploadVideoFinishBlock callback //上传视频结果callback, 注意:返回非主线程
返回:void
ex :

BytedCertTosInfo* tosInfo = [[BytedCertTosInfo alloc] initWithAccessKey:@"***" secretKey:@"***" stsToken:@"***" bucket:@"***" endpoint:@"tos-cn-beijing.volces.com" region:@"cjq-test.tos-cn-beijing.volces.com"];
[self.bytedCertWrapper configRecordeAndUploadParams:tosInfo callback:^(NSString* bytedToken, NSString * _Nonnull filePath, BytedCertError * _Nullable error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableString* message = [[NSMutableString alloc] init];
        if (error) {
            [message appendFormat:@"上传失败(%@): %@", @(error.errorCode), error.errorMessage];
        } else {
            [message appendFormat:@"上传成功:%@", filePath];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:filePath error:nil];
        }    
    });
}];

1.4 ocr识别接口

函数名:(void) doOCRH5:(BytedCertOCRResultBlock)callback
参数:
BytedCertOCRResultBlock callback //ocr识别结果callback
返回:void
ex :

[[BytedCertWrapper sharedInstance] doOCRH5:^(NSString* name, NSString* identityCode, BytedCertError *_Nullable error){
        //成功
        if (error.errorCode == 0){
            _codeField.text = identityCode;
            _nameField.text = name;
        }
        else {
            //失败
            UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:@"ocr结果"
                                                                           message:error.errorMessage
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
            }];
            [alertViewController addAction:okAction];
            
            [self presentViewController:alertViewController animated:YES completion:nil];
        }
    }];

1.5 获取token接口

函数原型:(void)startBytedToken:(BOOL)isRefSource

params:(NSDictionary*) params
   callback:(BytedCertBytedTokenResultBlock) callback;

参数:
isRefSource //true为有源比对,false为无源比对

params //活体参数,具体参数如下

idcard_name: 身份证名称, 有源比对为必传
idcard_no: 身份证id,有源比对为必传
binary_data_base64: 无源比对的基准图base64
motion_list:活体动作列表,以','隔开,例"0,1,2" //0:"眨眼",1:"张嘴",2:"点头",3:"摇头"
motion_count:需要的活体动作个数,从motion_list中选择motion_count个,需要小于等于motion_list中的个数
liveness_timeout: 端上活体超时时长,可选范围: [5, 60],默认:10
fixed_motion_list: 固定一定需要下发的动作列表, 取值同motion_list
max_liveness_trial: 端上动作活体最大尝试次数, 可选范围:[1, 100],默认:10

callback //返回token结果的callback
返回:void
ex:

//有源认证
NSMutableDictionary *reqtDict = [[NSMutableDictionary alloc] init];
[reqtDict setObject:@"0,1,2" forKey:@"motion_list"];
[reqtDict setObject:@"0" forKey:@"fixed_motion_list"];
[reqtDict setObject:@"10" forKey:@"liveness_timeout"];
[reqtDict setObject:@"10" forKey:@"max_liveness_trial"];
[reqtDict setObject:_nameField.text forKey:@"idcard_name"];
[reqtDict setObject:_codeField.text forKey:@"idcard_no"];
[[BytedCertWrapper sharedInstance] startBytedToken:YES params:reqtDict callback:^(NSString * _Nullable bytedToken, BytedCertError * _Nullable error){
    //bytedToken为token
    //error 错误信息,成功的话error为nil
}];

1.6 启动活体

/// 启动活体+回调活体信息
/// - Parameters:
///   - bytedToken: 从startBytedToken获取,也可以使用服务端中bytedToken中返回的
///   - clientConfig: 从startBytedToken中获取,也可使用服务端中的bytedToken中返回的
///   - options: 活体认证选项
///     - BytedCertParamVerifyNeeded: 是否需要进行比对
///     - BytedCertParamRefSource: 如需比对: "1"为有源,"0"为无源
///     - BytedCertParamTextSpeech: 是否开启语音播报, @(YES)/@(NO), 默认关闭
///   - callback: 活体结果
///     - data 结果信息 返回数据data格式参考身份认证sdk返回内容说明 中"服务端认证返回数据"章节的'jsonData'字段。注意:这里已把jsonData字符串解析成了NSDictionary格式
///       - facelive_info: 活体结果信息
///         - info: {data:xxx}
///     - error 错误信息,为nil表示成功
- (void)startFaceLive:(NSString *)bytedToken
         clientConfig:(NSString *)clientConfig
              options:(NSDictionary<BytedCertParamKey, id> *)options
             callback:(BytedCertFaceLivenessResultBlock)callback;

ex:

//有源认证

NSDictionary *options = @{
        BytedCertParamRefSource: @(1),
        BytedCertParamVerifyNeeded: @(YES)
    };
[[BytedCertWrapper sharedInstance] startFaceLive:bytedToken 
                                    clientConfig:clientConfig
                                         options:options 
                                        callback:^(NSDictionary *_Nullable data, BytedCertError *_Nullable error){
    NSString* title = @"人脸比对结果";
    NSMutableString* message = [[NSMutableString alloc] init];
    if (error) {
        [message appendFormat:@"失败: %@", error.errorMessage];
    }
    else{
        [message appendString:@"成功"];
    }

    UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
    }];
    [alertViewController addAction:okAction];
    [self presentViewController:alertViewController animated:YES completion:nil];
}];

1.7 启动认证接口

函数原型:(void)beginAuthorizationWithParams:(NSDictionary *)params
completion:(BytedCertFaceLivenessResultBlock)completion

参数:

params //活体参数

具体参数如下

ref_source:"1"为有源,"0"为无源,必传,目前只支持有源
motion_list:活体动作列表,以','隔开,例"0,1,2" //0:"眨眼",1:"张嘴",2:"点头",3:"摇头"
liveness_timeout: 端上活体超时时长,可选范围: [5, 60],默认:10
fixed_motion_list: 固定一定需要下发的动作列表, 取值同motion_list
max_liveness_trial: 端上动作活体最大尝试次数, 可选范围:[1, 100],默认:10

completion //认证结果callback
ex:

NSMutableDictionary *reqtDict = [[NSMutableDictionary alloc] init];
[reqtDict setObject:@"0,1,2" forKey:@"motion_list"];
[reqtDict setObject:@"0" forKey:@"fixed_motion_list"];
[reqtDict setObject:@"10" forKey:@"liveness_timeout"];
[reqtDict setObject:@"10" forKey:@"max_liveness_trial"];
//有源比对
[reqtDict setObject:@"1" forKey:@"ref_source"];
[[BytedCertWrapper sharedInstance] beginAuthorizationWithParams:reqtDict completion:^(NSDictionary * _Nullable data, BytedCertError * _Nullable error) {
    NSString* title = @"人脸比对结果";
    NSMutableString* message = [[NSMutableString alloc] init];
    if (error) {
        [message appendFormat:@"失败: %@", error.errorMessage];
    }
    else{
        [message appendString:@"成功"];
    }

    UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
    }];
    [alertViewController addAction:okAction];
    [self presentViewController:alertViewController animated:YES completion:nil];
    
}];

1.8 自定义身份证页面的用户协议

函数原型: (void)setUserProtocol:(NSString*)title linker:(NSString*)linker;

参数:
title //协议标题
linker //协议链接

ex:

[self.bytedCertWrapper setUserProtocol:titleTextField.text linker:linkerTextField.text];

1.9 自定义样式

1.9.1 获取样式对象单例

样式对象为bytedCertWrapper对象的uiConfig属性
属性原型:
@property (nonatomic, strong) BytedCertUIConfig *uiConfig;
ex:

BytedCertUIConfig* uiConfig = [[BytedCertWrapper sharedInstance] uiConfig];
1.9.2 BytedCertUIConfig详细说明
@interface BytedCertUIConfig : NSObject
+ (instancetype)sharedInstance;

@property (nonatomic, assign) BOOL useGuidePage;                        // 是否使用引导页,TRUE 使用, FALSE 不使用
@property (nonatomic, strong) NSString *protocolTitle;                  // 引导页用户协议标题
@property (nonatomic, strong) NSString *protocolLinker;                 // 引导页用户协议链接
@property (nonatomic, assign) BOOL *protocolNav;                        // 引导页用户协议是否展示协议导航栏
@property (nonatomic, strong) NSString *protocolNavTitle;               // 引导页用户协议导航栏标题
@property (nonatomic, strong) NSString *ocrProtocolTitle;               // OCR页用户协议标题
@property (nonatomic, strong) NSString *ocrProtocolLinker;              // OCR页用户协议链接
@property (nonatomic, assign) BOOL *ocrProtocolNav;                     // OCR页用户协议是否展示协议导航栏
@property (nonatomic, strong) NSString *ocrProtocolNavTitle;            // OCR页用户协议导航栏标题
@property (nonatomic, assign) UIStatusBarStyle statusBarStyle;          // 状态栏
@property (nonatomic, strong) UIColor *backgroundColor;                 // 背景色
@property (nonatomic, strong) UIColor *textColor;                       // 文字颜色
@property (nonatomic, strong) UIColor *timeColor;                       // 倒计时圈的颜色
@property (nonatomic, strong) UIColor *circleColor;                     // 圆圈的底色
@property (nonatomic, strong) UIFont *actionLabelFont;                  // 动作标签字体
@property (nonatomic, strong) UIImage *faceDetectionBgImage;            // 活体页面背景图
@property (nonatomic, assign) CGFloat faceDetectionProgressStrokeWidth; // 进度条宽度
@property (nonatomic, strong) UIImage *backBtnImage;                    // 返回按钮图片
@property (nonatomic, assign) int beautyIntensity; //预留字段不用设置 
@property (nonatomic, copy) NSDictionary *h5PageStyle;                  // h5页面样式配置,key-value形式

@end
  • useGuidePage 是否使用引导页,TRUE 使用, FALSE 不使用
  • protocolTitle 引导页用户协议标题
  • protocolLinker 引导页用户协议链接
  • protocolNav 引导页是否显示导航栏 TRUE 显示, FALSE 不显示
  • protocolNavTitle 引导页导航栏标题
  • ocrProtocolTitle OCR页用户协议标题
  • ocrProtocolLinker OCR页用户协议链接
  • ocrProtocolNav OCR页是否显示导航栏 TRUE 显示, FALSE 不显示
  • ocrProtocolNavTitle OCR页导航栏标题
  • statusBarStyle 活体页面状态栏
  • backgroundColor 活体页面背景色
  • textColor 活体页面文字颜色
  • timeColor 倒计时圆圈的颜色
  • circleColor 倒计时圆圈的底色
  • actionLabelFont 动作标签字体
  • faceDetectionProgressStrokeWidth 进度条宽度
  • h5PageStyle h5页面样式风格,key-value格式,详细见1.9.3(包括引导页以及身份证识别页面)
1.9.3 h5PageStyle格式说明
  • 主题色
    --theme: rgba(136, 91, 210, 1)
    --theme-btn_color: #fff

  • 遮罩背景色
    --mask_bg-color: rgba(56, 0, 128, 0.4)

  • 引导页
    --guide-btn_color: $theme-btn_color
    --guide-btn_bg-color: $theme
    --guide-sub-btn_color: $theme-btn_color
    --guide-sub-btn_bg-color: #2b2b2c
    --guide-protocol_color: $theme;

  • OCR上传身份证页面
    --ocr-camera-icon_color: $theme-btn_color;
    --ocr-camera-icon_bg-color: $theme;
    --ocr-btn_color: $theme-btn_color;
    --ocr-btn_bg-color: $theme;

1.9.4 示例
BytedCertUIConfig* uiConfig = [[BytedCertWrapper sharedInstance] uiConfig];
//自定义用户协议
uiConfig.protocolTitle = @"自定义协议";
uiConfig.protocolLinker = @"https://www.baidu.com";
//设置倒计时背景色
uiConfig.circleColor = [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
//设置倒计时进度条颜色
uiConfig.timeColor = [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
//设置引导页和身份证页面样式
NSDictionary *h5PageStyle = @{
            @"--theme" : @"rgba(136, 91, 210, 1)",
            @"--theme-btn_color" : @"#fff"
        };
uiConfig.h5PageStyle = h5PageStyle;

2. callback

2.1 BytedCertUploadVideoFinishBlock

//bytedToken: 视频对应bytedToken
//filePath: 录制的视频文件路径, 失败的话为nil
//error: 错误信息,为nil表示成功
typedef void (^BytedCertUploadVideoFinishBlock)(NSString* bytedToken, NSString* filePath, BytedCertError *_Nullable error);

2.2 BytedCertOCRResultBlock

//name 身份证名称
//identityCode 身份证号
//error 错误信息,为nil表示成功
typedef void  (^BytedCertOCRResultBlock)(NSString* name, NSString* identityCode, BytedCertError *_Nullable error);

2.3 BytedCertBytedTokenResultBlock

//bytedToken 返回的BytedToken值
//clientConfig 客户端配置,需要原样传入到startFaceLive接口中
//error 错误信息,为nil表示成功
typedef void (^BytedCertBytedTokenResultBlock)(NSString *_Nullable bytedToken, NSString *_Nullable clientConfig, BytedCertError *_Nullable error);

2.4 BytedCertFaceLivenessResultBlock

返回数据data格式参考SDK返回内容说明中"服务端认证返回数据"章节的'jsonData'字段。
注意:这里已把jsonData字符串解析成了NSDictionary格式

//data 返回数据
//error 错误信息,为nil表示成功
typedef void (^BytedCertFaceLivenessResultBlock)(NSDictionary *_Nullable data, BytedCertError *_Nullable error);
错误码
错误代码说明
0成功
-1000网络异常,请稍后再试
-1001未知错误
-1002活体中断
-1003活体识别失败,请再试一次
-1004算法初始化失败
-1005活体参数设置失败
-1006用户取消操作
-1007用户取消证件识别
-1101超过活体最大次数
-3001网络数据包解析失败
-3003无法使用相机,请检查是否打开相机权限
-3006相册图片获取失败
-3007拍照失败
-5021比对失败,请确认是本人
10000+10000以上错误码,为透传服务端的错误码,请参考人脸核身服务端api的错误码说明