Ionic
Capacitorプラグインを使用してIonicアプリにHuman Token Journeyを統合します。
この統合は、AndroidではCustom Tabs、iOSではASWebAuthenticationSessionに委譲するローカルCapacitorプラグインを使用します。ジャーニーが完了すると、ディープリンクが@capacitor/appを介して戻ります。
前提条件
- Ionic(Capacitor v4+)
@capacitor/appがインストール済み- Xcode 14+およびAndroid Studio
- アプリ固有のURLスキーム(例:
myapp)
ステップ 1:URLスキームを定義する
一意のスキームを選択します(例:myapp)。プロセス作成時にcallbackUriとして使用されます:
callbackUri: myapp://callback
Human Plugin APIでプロセスを作成し、この値をcallbackUriとして送信します。プロセスの作成を参照してください。
ステップ 2:Capacitorプラグインインターフェースを作成する
import { registerPlugin } from '@capacitor/core';
export interface UnicoPlugin {
openSession(options: { url: string }): Promise<{ callbackUrl: string }>;
}
const Unico = registerPlugin<UnicoPlugin>('UnicoPlugin');
export default Unico;
ステップ 3:iOSプラグインを実装する
import Capacitor
import AuthenticationServices
@objc(UnicoPlugin)
public class UnicoPlugin: CAPPlugin, ASWebAuthenticationPresentationContextProviding {
private var session: ASWebAuthenticationSession?
@objc func openSession(_ call: CAPPluginCall) {
guard let urlString = call.getString("url"),
let url = URL(string: urlString) else {
call.reject("Invalid URL")
return
}
DispatchQueue.main.async {
let newSession = ASWebAuthenticationSession(
url: url,
callbackURLScheme: "myapp"
) { callbackURL, error in
self.session = nil
if let callbackURL = callbackURL {
call.resolve(["callbackUrl": callbackURL.absoluteString])
} else {
call.reject(error?.localizedDescription ?? "Session cancelled")
}
}
newSession.presentationContextProvider = self
newSession.prefersEphemeralWebBrowserSession = true
newSession.start()
self.session = newSession
}
}
public 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
}
}