iOS
Intégrez le parcours Human Token dans votre application iOS avec ASWebAuthenticationSession.
ASWebAuthenticationSession lance le parcours Human Token dans un navigateur géré par le système. Lorsque le parcours est terminé, la session retourne à votre application via le schéma d'URL enregistré.
Prérequis
- iOS 13 ou ultérieur
- Xcode 14+
- Framework
AuthenticationServices(intégré) - Un schéma d'URL unique pour votre application (par ex.
myapp)
Étape 1 : Définir votre schéma d'URL
Choisissez un schéma unique (par ex. myapp). Il est utilisé comme callbackUri lors de la création du processus :
callbackUri: myapp://callback
Étape 2 : Créer le fournisseur de contexte de présentation
import AuthenticationServices
import UIKit
class UnicoAuthenticationController: 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
}
}
Étape 3 : Créer le gestionnaire de session
import AuthenticationServices
class AuthenticationSessionManager {
static let shared = AuthenticationSessionManager()
private var session: ASWebAuthenticationSession?
private let contextProvider = UnicoAuthenticationController()
func start(url: URL, completion: @escaping (String?) -> Void) {
let newSession = ASWebAuthenticationSession(
url: url,
callbackURLScheme: "myapp"
) { callbackURL, error in
self.session = nil
if let callbackURL = callbackURL {
let components = URLComponents(url: callbackURL, resolvingAgainstBaseURL: false)
let processId = components?.queryItems?.first(where: { $0.name == "id" })?.value
completion(processId)
} else {
completion(nil)
}
}
newSession.presentationContextProvider = contextProvider
newSession.prefersEphemeralWebBrowserSession = true
newSession.start()
session = newSession
}
}
Étape 4 : Lancer le parcours
guard let url = URL(string: processLink) else { return }
AuthenticationSessionManager.shared.start(url: url) { processId in
if let processId = processId {
// Journey completed — send processId to your backend via Get Process
} else {
// User canceled the session
}
}
info
Le paramètre callbackURLScheme n'accepte que le nom du schéma (myapp), sans ://.
Résumé des fichiers
| Fichier | Objectif |
|---|---|
UnicoAuthenticationController.swift | Fournisseur de contexte de présentation |
| Gestionnaire de session (votre vue ou service) | Cycle de vie de ASWebAuthenticationSession |