Flutter कैप्चर
platform channel के साथ Flutter ऐप से कैप्चर खोलें।
आपका बैक-एंड Human Plugin में Journey flow और ऐसे callbackUri के साथ process बनाता है जो आपके ऐप की URL scheme का उपयोग करता है। एक MethodChannel जवाब के link को iOS पर ASWebAuthenticationSession में और Android पर Custom Tabs में खोलता है। किसी बाहरी पैकेज की ज़रूरत नहीं है।
create के जवाब से Human Plugin का id रखें। आपका बैक-एंड उसी से नतीजा पढ़ता है।
पूर्वापेक्षाएं
- Flutter 3 या बाद का
- iOS के लिए Xcode 14 या बाद का
- Android के लिए Android Studio
- आपके ऐप की अपनी यूनीक URL scheme, जैसे
myapp
1. अपनी scheme के साथ process बनाएं
आपका बैक-एंड scheme को callbackUri के रूप में भेजता है:
{
"flow": "humantoken",
"clientId": "client-a",
"correlationId": "order-1001",
"credential": { "type": "DUI_TYPE_NG_BVN", "value": "<document number>" },
"callbackUri": "myapp://callback"
}
और id तथा link ऐप को लौटाता है।
2. Dart सर्विस बनाएं
// lib/services/capture_service.dart
import 'package:flutter/services.dart';
class CaptureService {
static const _channel = MethodChannel('capture_plugin');
/// Returns true when the capture ended, false when the person closed it.
Future<bool> open(String link) async {
try {
final result = await _channel.invokeMethod<String>('openSession', {'url': link});
return result != null;
} on PlatformException {
return false;
}
}
}
3. iOS plugin इम्प्लीमेंट करें
// ios/Runner/CapturePlugin.swift
import Flutter
import AuthenticationServices
class CapturePlugin: NSObject, FlutterPlugin, ASWebAuthenticationPresentationContextProviding {
private var session: ASWebAuthenticationSession?
static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "capture_plugin", binaryMessenger: registrar.messenger())
let instance = CapturePlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard call.method == "openSession",
let args = call.arguments as? [String: Any],
let urlString = args["url"] as? String,
let url = URL(string: urlString) else {
result(FlutterMethodNotImplemented)
return
}
let newSession = ASWebAuthenticationSession(
url: url,
callbackURLScheme: "myapp"
) { callbackURL, error in
self.session = nil
if let callbackURL = callbackURL {
result(callbackURL.absoluteString)
} else {
result(FlutterError(code: "SESSION_ERROR",
message: error?.localizedDescription ?? "Session cancelled",
details: nil))
}
}
newSession.presentationContextProvider = self
newSession.prefersEphemeralWebBrowserSession = true
newSession.start()
session = newSession
}
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
}
}
इसे AppDelegate.swift में रजिस्टर करें:
CapturePlugin.register(with: self.registrar(forPlugin: "CapturePlugin")!)
4. Android plugin इम्प्लीमेंट करें
android/app/build.gradle में dependency जोड़ें:
implementation("androidx.browser:browser:1.5.0")
// android/app/src/main/kotlin/<your.package>/CapturePlugin.kt
package com.yourapp
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
class CapturePlugin : FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware {
private var channel: MethodChannel? = null
private var binding: ActivityPluginBinding? = null
private var pendingResult: MethodChannel.Result? = null
override fun onAttachedToEngine(flutterBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterBinding.binaryMessenger, "capture_plugin")
channel?.setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (call.method == "openSession") {
val url = call.argument<String>("url") ?: return result.error("MISSING_URL", "URL is required", null)
pendingResult = result
val activity = binding?.activity ?: return result.error("NO_ACTIVITY", "No activity", null)
CustomTabsIntent.Builder().build().launchUrl(activity, Uri.parse(url))
} else {
result.notImplemented()
}
}
fun handleRedirect(uri: Uri) {
if (uri.scheme == "myapp") {
pendingResult?.success(uri.toString())
pendingResult = null
}
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) { this.binding = binding }
override fun onDetachedFromActivity() { binding = null }
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { this.binding = binding }
override fun onDetachedFromActivityForConfigChanges() { binding = null }
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { channel = null }
}
इसे रजिस्टर करें और वापसी को MainActivity.kt में संभालें:
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
flutterEngine.plugins.add(CapturePlugin())
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
intent.data?.let { uri ->
if (uri.scheme == "myapp") {
val plugin = flutterEngine?.plugins?.get(CapturePlugin::class.java) as? CapturePlugin
plugin?.handleRedirect(uri)
}
}
}
5. अपनी scheme रजिस्टर करें
- iOS
- Android
Info.plist में:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
AndroidManifest.xml में, अपनी मुख्य <activity> के अंदर:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="callback" />
</intent-filter>
6. कैप्चर शुरू करें
final ended = await CaptureService().open(link);
if (ended) {
// The capture ended. Ask your back-end to read the outcome
// with GET /v1/processes/{id}, using the Human Plugin id.
} else {
// The person closed the session.
}
सारांश
| फ़ाइल | उद्देश्य |
|---|---|
lib/services/capture_service.dart | Dart MethodChannel सर्विस |
ios/Runner/CapturePlugin.swift | iOS: ASWebAuthenticationSession |
ios/Runner/AppDelegate.swift | Plugin रजिस्ट्रेशन |
android/.../CapturePlugin.kt | Android: Custom Tabs |
android/.../MainActivity.kt | Plugin रजिस्ट्रेशन और वापसी की हैंडलिंग |
Info.plist | iOS URL scheme |
AndroidManifest.xml | Android intent filter |