Lewati ke konten utama
iOS

Integrasikan Human Token Journey ke dalam aplikasi iOS Anda menggunakan ASWebAuthenticationSession.

Markdown

ASWebAuthenticationSession meluncurkan Human Token Journey di browser yang dikelola sistem. Saat perjalanan selesai, sesi kembali ke aplikasi Anda melalui skema URL yang terdaftar.

Prasyarat​

  • iOS 13 atau lebih baru
  • Xcode 14+
  • Framework AuthenticationServices (bawaan)
  • Skema URL unik untuk aplikasi Anda (misalnya myapp)

Langkah 1: Tentukan skema URL Anda​

Pilih skema unik (misalnya myapp). Ini digunakan sebagai callbackUri saat membuat proses:

callbackUri: myapp://callback

Langkah 2: Buat presentation context provider​

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

Langkah 3: Buat session manager​

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

Langkah 4: Mulai perjalanan​

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

Parameter callbackURLScheme hanya menerima nama skema (myapp), tanpa ://.

Ringkasan file​

FileTujuan
UnicoAuthenticationController.swiftPresentation context provider
Session manager (view atau service Anda)Siklus hidup ASWebAuthenticationSession