Zum Hauptinhalt springen
Ionic

Integrieren Sie die Human Token Journey in Ihre Ionic-App mit einem Capacitor-Plugin.

Markdown

Die Integration verwendet ein lokales Capacitor-Plugin, das an Custom Tabs auf Android und ASWebAuthenticationSession auf iOS delegiert. Wenn die Journey abgeschlossen ist, kehrt der Deep Link über @capacitor/app zurück.

Voraussetzungen​

  • Ionic mit Capacitor v4+
  • @capacitor/app installiert
  • Xcode 14+ und Android Studio
  • Ein eindeutiges URL-Schema für Ihre App (z. B. myapp)

Schritt 1: URL-Schema definieren​

Wählen Sie ein eindeutiges Schema (z. B. myapp). Dies wird als callbackUri beim Erstellen des Prozesses verwendet:

callbackUri: myapp://callback

Schritt 2: Capacitor-Plugin-Interface erstellen​

import { registerPlugin } from '@capacitor/core';

export interface UnicoPlugin {
openSession(options: { url: string }): Promise<{ callbackUrl: string }>;
}

const Unico = registerPlugin<UnicoPlugin>('UnicoPlugin');
export default Unico;

Schritt 3: iOS-Plugin implementieren​

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

Schritt 4: Android-Plugin implementieren​

import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import com.getcapacitor.Plugin
import com.getcapacitor.PluginCall
import com.getcapacitor.PluginMethod
import com.getcapacitor.annotation.CapacitorPlugin

@CapacitorPlugin(name = "UnicoPlugin")
class UnicoPlugin : Plugin() {

private var pendingCall: PluginCall? = null

@PluginMethod
fun openSession(call: PluginCall) {
val url = call.getString("url") ?: return call.reject("Missing URL")
pendingCall = call

activity.runOnUiThread {
CustomTabsIntent.Builder().build()
.launchUrl(activity, Uri.parse(url))
}
}

override fun handleOnNewIntent(intent: Intent) {
super.handleOnNewIntent(intent)
intent.data?.let { uri ->
if (uri.scheme == "myapp") {
pendingCall?.resolve(com.getcapacitor.JSObject().put("callbackUrl", uri.toString()))
pendingCall = null
}
}
}
}

Schritt 5: URL-Schemata konfigurieren​

Fügen Sie zu Info.plist hinzu:

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>

Schritt 6: Angular-Service erstellen​

import { Injectable } from '@angular/core';
import Unico from '../plugins/unico-plugin';

@Injectable({ providedIn: 'root' })
export class UnicoService {
async openSession(url: string): Promise<string | null> {
try {
const result = await Unico.openSession({ url });
const params = new URL(result.callbackUrl).searchParams;
return params.get('id');
} catch {
return null;
}
}
}

In Ihrer AppComponent:

import { App } from '@capacitor/app';

App.addListener('appUrlOpen', (event) => {
// Handle the callback URL if needed
console.log('Deep link received:', event.url);
});

Dateiübersicht​

DateiZweck
unico-plugin.tsCapacitor-Plugin-Interface
UnicoPlugin.swiftiOS — ASWebAuthenticationSession
UnicoPlugin.ktAndroid — Custom Tabs
Info.plistiOS-URL-Schema
AndroidManifest.xmlAndroid-Intent-Filter
unico.service.tsAngular-Service-Wrapper