Captura no iOS
Abra a captura em uma sessão de navegador do sistema e traga a pessoa de volta ao seu app.
Seu back-end cria o processo no Human Plugin com um fluxo Journey e um callbackUri que usa o URL scheme do seu app. O app abre o link da resposta em uma ASWebAuthenticationSession. Quando a captura termina, a sessão retorna ao app pelo seu scheme.
Guarde o id do Human Plugin retornado pelo create. Seu back-end lê o resultado com ele.
Pré-requisitos
- iOS 13 ou superior
- Xcode 14 ou superior
- O framework
AuthenticationServices(nativo) - Um URL scheme exclusivo do seu app, como
myapp
1. Crie o processo com o seu scheme
Seu back-end envia o scheme como callbackUri:
{
"flow": "humantoken",
"clientId": "client-a",
"correlationId": "order-1001",
"credential": { "type": "DUI_TYPE_NG_BVN", "value": "<document number>" },
"callbackUri": "myapp://callback"
}
e devolve id e link ao app.
2. Crie o 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. Crie o gerenciador de sessão
import AuthenticationServices
class CaptureSessionManager {
static let shared = CaptureSessionManager()
private var session: ASWebAuthenticationSession?
private let contextProvider = CaptureContextProvider()
/// Chama completion com true quando a captura terminou, false quando a pessoa a fechou.
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
}
}
informação
callbackURLScheme recebe apenas o nome do scheme (myapp), sem ://.
4. Inicie a captura
guard let url = URL(string: link) else { return }
CaptureSessionManager.shared.start(link: url) { ended in
if ended {
// A captura terminou. Peça ao seu back-end para ler o resultado
// com GET /v1/processes/{id}, usando o id do Human Plugin.
} else {
// A pessoa fechou a sessão.
}
}
Resumo
| Arquivo | Finalidade |
|---|---|
CaptureContextProvider.swift | Presentation context provider |
CaptureSessionManager.swift | Ciclo de vida da ASWebAuthenticationSession |