I am implementing face liveness detector for detecting real profile and detect fake profile for one of my projects. So face liveness detector library is only present in native ios and I installed is through spm packages of swift. I successfully rendered that SwiftUI using method channels of flutter. But I am stucked at one point that when face liveness completes its working after that I want to pass the result in form of strings. and I do not know how to do it in native Ios swift code as I am very new to swift language as well as its core concepts. I tried using delegate pattern swift but no luck I am not getting result in my ViewControllers. Anyone please help me as I am stucked in this problem for more than week. This is how write my code in swift.
import UIKit
import Flutter
import Amplify
import AWSCognitoAuthPlugin
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, FaceLivenessResultDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: "LIVE_VERIFICATION_CHANNEL", binaryMessenger: controller.binaryMessenger)
methodChannel.setMethodCallHandler { [weak self] (call, result) in
if call.method == "startLiveVerification" {
guard let args = call.arguments as? [String: Any],
let sessionId = args["sessionId"] as? String else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "Invalid session Id",details: nil))
return
}
print("session id is \(sessionId)")
let faceLivenessController = FaceLiveDetectionController(sessionId: sessionId)
controller.present(faceLivenessController,
animated: true,
completion: nil)
} else {
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func resultCallback(_ result: String) {
print("you are in result \(result)")
}
}
import Foundation
import SwiftUI
class FaceLiveDetectionController: UIHostingController<FaceLivenessDetectionView>, FaceLivenessDelgate {
var sessionId: String?
var delegate: FaceLivenessResultDelegate?
func verificationCallback(_ result: String) {
delegate?.resultCallback(result)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.sessionId = nil
rootView = FaceLivenessDetectionView(
sessionId: sessionId
)
}
init(sessionId: String) {
self.sessionId = sessionId
super.init(
rootView: FaceLivenessDetectionView(
sessionId: sessionId
)
)
}
func facelivenessCallback(_ result: String) {
print("you are here--> \(result)")
verificationCallback(result)
}
}
import SwiftUI
import FaceLiveness
struct FaceLivenessDetectionView: View {
@State private var isPresentingLiveness = true
var sessionId: String?
weak var delegate: FaceLivenessDelgate?
var body: some View {
if let livenessSessionId = sessionId {
FaceLivenessDetectorView(
sessionID: livenessSessionId,
region: "ap-south-1",
disableStartView: true,
isPresented: $isPresentingLiveness,
onCompletion: { result in
switch result {
case .success:
delegate?.facelivenessCallback("success")
print("you are in success -->")
case .failure(let error):
delegate?.facelivenessCallback("error")
print("you are in error")
default:
delegate?.facelivenessCallback("error")
print("Face Liveness Session default")
}
}
)
} else {
Text("Liveness session not started")
.font(.largeTitle)
.foregroundColor(.white)
.background(Color.blue)
.edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
}
}
init(sessionId: String?) {
self.sessionId = sessionId
}
}