Pular para o conteúdo principal
iOS

Integre a jornada Human Token no seu aplicativo iOS usando ASWebAuthenticationSession.

Markdown

ASWebAuthenticationSession inicia a jornada Human Token em um navegador gerenciado pelo sistema. Quando a jornada termina, a sessão retorna ao seu aplicativo pelo esquema de URL registrado.

Pré-requisitos​

  • iOS 13 ou posterior
  • Xcode 14+
  • Framework AuthenticationServices (integrado)
  • Um esquema de URL exclusivo para o seu aplicativo (ex.: myapp)

Passo 1: Definir o esquema de URL​

Escolha um esquema exclusivo (ex.: myapp). Ele é usado como callbackUri ao criar o processo:

callbackUri: myapp://callback

Passo 2: Criar o provedor de contexto de apresentação​

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

Passo 3: Criar o gerenciador de sessão​

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

Passo 4: Iniciar a 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
}
}
informação

O parâmetro callbackURLScheme aceita apenas o nome do esquema (myapp), sem ://.

Resumo dos arquivos​

ArquivoFinalidade
UnicoAuthenticationController.swiftProvedor de contexto de apresentação
Gerenciador de sessão (sua view ou serviço)Ciclo de vida do ASWebAuthenticationSession