Saltar al contenido principal
iOS

Integra la jornada Human Token en tu aplicación iOS usando ASWebAuthenticationSession.

Markdown

ASWebAuthenticationSession inicia la jornada Human Token en un navegador administrado por el sistema. Cuando la jornada finaliza, la sesión regresa a tu aplicación a través del esquema de URL registrado.

Requisitos previos​

  • iOS 13 o posterior
  • Xcode 14+
  • Framework AuthenticationServices (integrado)
  • Un esquema de URL único para tu aplicación (por ejemplo, myapp)

Paso 1: Definir tu esquema de URL​

Elige un esquema único (por ejemplo, myapp). Se usa como callbackUri al crear el proceso:

callbackUri: myapp://callback

Paso 2: Crear el proveedor de contexto de presentación​

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

Paso 3: Crear el administrador de sesión​

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

Paso 4: Iniciar la jornada​

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
}
}
información

El parámetro callbackURLScheme solo acepta el nombre del esquema (myapp), sin ://.

Resumen de archivos​

ArchivoPropósito
UnicoAuthenticationController.swiftProveedor de contexto de presentación
Administrador de sesión (tu vista o servicio)Ciclo de vida de ASWebAuthenticationSession