Zum Hauptinhalt springen
iOS

Integrieren Sie die Human Token Journey in Ihre iOS-App mit ASWebAuthenticationSession.

Markdown

ASWebAuthenticationSession startet die Human Token Journey in einem systemverwalteten Browser. Wenn die Journey abgeschlossen ist, kehrt die Sitzung über das registrierte URL-Schema zu Ihrer App zurück.

Voraussetzungen​

  • iOS 13 oder höher
  • Xcode 14+
  • AuthenticationServices-Framework (integriert)
  • Ein eindeutiges URL-Schema für Ihre App (z. B. myapp)

Schritt 1: URL-Schema definieren​

Wählen Sie ein eindeutiges Schema (z. B. myapp). Dies wird als callbackUri beim Erstellen des Prozesses verwendet:

callbackUri: myapp://callback

Schritt 2: Presentation Context Provider erstellen​

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
}
}

Schritt 3: Session-Manager erstellen​

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
}
}

Schritt 4: Journey starten​

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
}
}
Information

Der Parameter callbackURLScheme nimmt nur den Schema-Namen (myapp) entgegen, ohne ://.

Dateiübersicht​

DateiZweck
UnicoAuthenticationController.swiftPresentation Context Provider
Session-Manager (Ihre View oder Ihr Service)ASWebAuthenticationSession-Lebenszyklus