iOS 采集
在系统浏览器会话中打开采集,并将用户带回您的应用。
您的后端使用 Journey 流程和一个采用您应用 URL scheme 的 callbackUri 在 Human Plugin 中创建流程。应用在 ASWebAuthenticationSession 中打开响应里的 link。采集结束后,会话通过您的 scheme 返回应用。
请保留创建响应中的 Human Plugin id。您的后端用它来读取结果。
前提条件
- iOS 13 或更高版本
- Xcode 14 或更高版本
AuthenticationServices框架(系统内置)- 您应用独有的 URL scheme,例如
myapp
1. 使用您的 scheme 创建流程
您的后端将该 scheme 作为 callbackUri 发送:
{
"flow": "humantoken",
"clientId": "client-a",
"correlationId": "order-1001",
"credential": { "type": "DUI_TYPE_NG_BVN", "value": "<document number>" },
"callbackUri": "myapp://callback"
}
并将 id 和 link 返回给应用。
2. 创建 presentation context provider
import AuthenticationServices
import UIKit
class CaptureContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first else {
return ASPresentationAnchor()
}
return window
}
}
3. 创建会话管理器
import AuthenticationServices
class CaptureSessionManager {
static let shared = CaptureSessionManager()
private var session: ASWebAuthenticationSession?
private let contextProvider = CaptureContextProvider()
/// Calls completion with true when the capture ended, false when the person closed it.
func start(link: URL, completion: @escaping (Bool) -> Void) {
let newSession = ASWebAuthenticationSession(
url: link,
callbackURLScheme: "myapp"
) { callbackURL, _ in
self.session = nil
completion(callbackURL != nil)
}
newSession.presentationContextProvider = contextProvider
newSession.prefersEphemeralWebBrowserSession = true
newSession.start()
session = newSession
}
}
信息
callbackURLScheme 只接受 scheme 名称(myapp),不带 ://。
4. 启动采集
guard let url = URL(string: link) else { return }
CaptureSessionManager.shared.start(link: url) { ended in
if ended {
// The capture ended. Ask your back-end to read the outcome
// with GET /v1/processes/{id}, using the Human Plugin id.
} else {
// The person closed the session.
}
}
总结
| 文件 | 用途 |
|---|---|
CaptureContextProvider.swift | Presentation context provider |
CaptureSessionManager.swift | ASWebAuthenticationSession 生命周期 |