Ver-ID SDK for iOS
先决条件
最低 iOS 版本为 12。
为了构建此项目并运行示例应用,您需要一个带有以下应用的 Apple Mac 计算机
安装
-
打开 终端 并输入以下命令
git clone https://github.com/AppliedRecognition/Ver-ID-UI-iOS.git cd Ver-ID-UI-iOS pod install git lfs install git lfs pull open VerIDUI.xcworkspace
-
VerIDUI.xcworkspace 应现在在 Xcode 中打开。
-
将所有目标上 签名 & 权限 选项卡中的 Team 设置更改为。
-
现在您可以在您的 iOS 设备上构建和运行 Ver-ID 示例 目标。
将 Ver-ID 添加到您的个人项目中
-
注册您的应用程序(需要您的应用程序的包标识符)。
-
注册您的应用程序将为您的应用程序生成一个评估许可证。许可证有效期30天。如需生产许可证,请联系 Applied Recognition。
-
完成注册后,您将收到一个名为 Ver-ID identity.p12 的文件和一个密码。将密码复制到安全位置,并在您的应用程序中添加 Ver-ID identity.p12 文件。
- 在 Xcode 中打开您的项目。
- 从顶部菜单选择 文件/将文件添加到 “[您的项目名称]”... 或按 ⌥⌘A 并浏览以选择下载的 Ver-ID identity.p12 文件。
- 点击对话框左下角的 选项 按钮以显示选项。
- 在 目标 下的 如有必要则复制项目 复选框。
- 在 添加到目标 下选择您的应用程序目标。
-
Ver-ID 需要在注册时收到的密码来构建
VerIDSDKIdentity
的实例。-
您可以在应用程序的 Info.plist 中添加密码。
<key>com.appliedrec.verid.password</key> <string>your password goes here</string>
-
或者在创建
VerIDFactory
实例时指定密码let identity = try VerIDIdentity(password: "your password goes here")
并将
VerIDIdentity
的实例传递给VerIDFactory
构造函数let veridFactory = VerIDFactory(identity: identity)
-
-
如果您的项目使用 CocoaPods 进行依赖项管理,请打开项目的 Podfile。否则请确保已安装 CocoaPods,并在项目文件夹中创建一个名为 Podfile 的文件(不包含扩展名)。
-
假设您的项目名为 MyProject,它有一个名为 MyApp 的应用程序目标。在文本编辑器中打开 Podfile 并输入以下内容
project 'MyProject.xcodeproj' workspace 'MyProject.xcworkspace' platform :ios, '11' target 'MyApp' do use_frameworks! pod 'Ver-ID' # The following script sets the BUILD_LIBRARY_FOR_DISTRIBUTION build setting # to YES and removes the setting from the pod dependencies. Without it the # project will compile but it will fail to run. post_install do |installer| installer.pods_project.build_configurations.each do |config| config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES' end installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings.delete 'BUILD_LIBRARY_FOR_DISTRIBUTION' end end end end
请确保您包括post_install
脚本。没有它,您的应用程序会崩溃。 -
保存 Podfile。打开 终端,导航到您的项目文件夹。然后输入
pod install
-
现在您可以在 Xcode 中打开 MyProject.xcworkspace,并在应用程序 MyApp 中使用 Ver-ID。
-
在开始 Ver-ID 会话之前,请确保您的应用程序在 Info.plist 文件中声明了相机权限。
<key>NSCameraUsageDescription</key> <string>Your reason for requesting camera permission</string>
库初始化
使用回调(自2.0.0新引入)
这是获取VerID
实例的最简单方法
VerIDFactory().createVerID { result in
switch result {
case .success(let verID):
// VerID created
break
case .failure(let error):
// VerID creation failed
break
}
}
使用代理
该方法自Ver-ID SDK的1版本起就是一部分
class MyClass: NSObject, VerIDFactoryDelegate {
func loadVerID() {
let veridFactory = VerIDFactory()
veridFactory.delegate = self
veridFactory.createVerID()
}
// MARK: - Ver-ID factory delegate methods
func veridFactory(_ factory: VerIDFactory, didCreateVerID verID: VerID) {
// VerID created
}
func veridFactory(_ factory: VerIDFactory, didFailWithError error: Error) {
// VerID creation failed
}
}
同步
如果你的应用程序在后台队列上做其他工作,你可能希望同步地加载库。
注意:永远不要在主队列上调用此方法。在主队列上调用此方法会导致你的应用程序在方法返回之前无响应。
DispatchQueue.global().async {
do {
let verID = try VerIDFactory().createVerIDSync()
// VerID created
} catch {
// VerID creation failed
}
}
运行 Ver-ID 会话
- 在运行 Ver-ID UI 会话之前,您需要导入
VerIDCore
框架并创建一个VerID
实例。 - 让您的类实现
VerIDFactoryDelegate
协议。当创建VerID
实例或创建失败时,您将收到回调。 - 在运行 Ver-ID 会话的类中导入
VerIDUI
。 - 将
VerID
实例和会话设置传递给VerIDSession
构造函数。
即时活体检测
Ver-ID 2.7.0 支持不要求用户转头即可进行活体检测。
即时活体检测适用于运行 iOS 13 或更新版本的设备。默认会话设置构造函数将在支持即时活体的系统上将所需的 face captures 数量设置为 1,在其他系统上设置为 2。您可以构建自己的逻辑来决定是否要求用户进行超过一个的 face captures(头部姿势)。
例如,若要在即时活体检测可用时请求 2 个头部姿势,而在不可用时请求 3 个,请使用
let settings = LivenessDetectionSessionSettings()
settings.faceCaptureCount = VerIDSessionSettings.supportsInstantLiveness ? 2 : 3
会话示例
以下示例加载 Ver-ID 并运行一个活体检测会话。示例显示了 VerIDSessionDelegate
中可用的方法。
import UIKit
import VerIDCore
import VerIDUI
class MyViewController: UIViewController, VerIDSessionDelegate {
func runLivenessDetection() {
// You may want to display an activity indicator as the instance creation may take up to a few seconds
// Create an instance of Ver-ID
VerIDFactory().createVerID { result in
switch result {
case .success(let verID):
// Ver-ID instance was created
// Create liveness detection settings
let settings = LivenessDetectionSessionSettings()
// Create a Ver-ID UI session
let session = VerIDSession(environment: verID, settings: settings)
// Set your class as a delegate of the session to receive the session outcome
session.delegate = self
// Start the session
session.start()
break
case .failure(let error):
NSLog("Failed to create Ver-ID instance: %@", error.localizedDescription)
break
}
}
}
// MARK: - Session delegate
func didFinishSession(_ session: VerIDSession, withResult result: VerIDSessionResult) {
// Session finished successfully
}
// MARK: Optional session delegate methods
func didCancelSession(_ session: VerIDSession) {
// Session was canceled
}
func shouldDisplayResult(_ result: VerIDSessionResult, ofSession session: VerIDSession) -> Bool {
// Return `true` to display the result of the session
return true
}
func shouldSpeakPromptsInSession(_ session: VerIDSession) -> Bool {
// Return `true` to speak prompts in the session
return true
}
func shouldRecordVideoOfSession(_ session: VerIDSession) -> Bool {
// Return `true` to record a video of the session
return true
}
func cameraPositionForSession(_ session: VerIDSession) -> AVCaptureDevice.Position {
// Return `AVCaptureDevice.Position.back` to use the back camera instead of the front (selfie) camera
return .back
}
var runCount = 0
func shouldRetrySession(_ session: VerIDSession, afterFailure error: Error) -> Bool {
// Return `true` to allow the user to retry the session on failure
// For example, you can keep track of how many times the user tried and fail the session on Xth attempt
runCount += 1
return runCount < 3
}
func shouldDisplayCGHeadGuidanceInSession(_ session: VerIDSession) -> Bool {
// Return `false` to disable "3D head" guidance
return false
}
}