الانتقال إلى المحتوى الرئيسي
iOS

قم بدمج رحلة Human Token في تطبيق iOS الخاص بك باستخدام ASWebAuthenticationSession.

Markdown

يقوم ASWebAuthenticationSession بتشغيل رحلة Human Token في متصفح يديره النظام. عند انتهاء الرحلة، تعود الجلسة إلى تطبيقك عبر مخطط URL المسجل.

المتطلبات الأساسية​

  • iOS 13 أو أحدث
  • Xcode 14+
  • إطار عمل AuthenticationServices (مدمج)
  • مخطط URL فريد لتطبيقك (مثل myapp)

الخطوة 1: تحديد مخطط URL الخاص بك​

اختر مخططًا فريدًا (مثل myapp). يُستخدم هذا كـ callbackUri عند إنشاء العملية:

callbackUri: myapp://callback

الخطوة 2: إنشاء مزود سياق العرض​

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

الخطوة 3: إنشاء مدير الجلسة​

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

الخطوة 4: بدء الرحلة​

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
}
}
معلومة

يأخذ معامل callbackURLScheme اسم المخطط فقط (myapp)، بدون ://.

ملخص الملفات​

الملفالغرض
UnicoAuthenticationController.swiftمزود سياق العرض
مدير الجلسة (العرض أو الخدمة الخاصة بك)دورة حياة ASWebAuthenticationSession