Peter Wilson
Peter Wilson

Reputation: 4319

Flutter adding new ViewController for BlinkId plugin

I am working on a flutter app and I have tp integrate BlinkId plugin to scan documents which doesn't have flutter plugin so I used MethodChannel to invoke a method on the native code then I tried to add the native code of the plugin.

everything working and the view of the plugin is opening and scanning is done successfully but the scanning view doesn't close at all even if I click close which suppose to cancel the scanning.

non of the methods seems to be working except viewdidload but didfinishscanning or didtabclose are not working.

here is my code:

import UIKit
import Flutter
import GoogleMaps
import Microblink
var _result: FlutterResult?

@UIApplicationMain
 @objc class AppDelegate: FlutterAppDelegate {

override func application(
 _ application: UIApplication,
 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
 ) -> Bool {
  let controller : FlutterViewController = window?.rootViewController as! FlutterViewController



  let scanIdChannel = FlutterMethodChannel(name: "native.wasfago.scanId",
                                            binaryMessenger: controller.binaryMessenger)
  scanIdChannel.setMethodCallHandler({
  (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
    // Note: this method is invoked on the UI thread.
    guard call.method == "scanId" else {
      result(FlutterMethodNotImplemented)
      return
    }
    _result = result
    self.scanId(result: result) // handle click event from flutter button
    print("clicked!")

  })
  GeneratedPluginRegistrant.register(with: self)
  return super.application(application, didFinishLaunchingWithOptions: launchOptions)
 }
 private func scanId(result: FlutterResult) {

    let viewCtrl = ViewController()
    topMostController().view.addSubview(viewCtrl.view)

 }
 func topMostController() -> UIViewController {
       var topController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
       while ((topController?.presentedViewController) != nil) {
           topController = topController?.presentedViewController

       }
       return topController!
   }
}

class ViewController: UIViewController ,MBBlinkIdOverlayViewControllerDelegate{
   func blinkIdOverlayViewControllerDidTapClose(_ blinkIdOverlayViewController: MBBlinkIdOverlayViewController) {
    print("Closed!!!!!!!!!")
   }


   var blinkIdRecognizer : MBBlinkIdCombinedRecognizer?

   override func viewDidLoad() {
      super.viewDidLoad()
      // Valid until: 2020-06-26

      self.didTapScan()
      print("View Loaded!")
   }

   @IBAction func didTapScan() {
MBMicroblinkSDK.sharedInstance().setLicenseKey("sRwAAAEPY29tLnczNC53YXNmYWdvfIRuYWSSC81qt+lUDRzpTwtWuUsIPrIHmH2dNCTjx5qYKCfr3nKw9UVE7TIRv2nq/jDlTtqhcVZA+4dyVG8moP4DeOygPcRAkdy6L+WpNhacuZMjrTAUmGwooe3CSzaj8D8Y6Znf98SHVIE9bxdSv23SOfCQnNsoCSksIYvpjVjpT5DUExr6qSY+QqeH3EUxDR9GqIPgeiEGIXZUeOdqnIyNiGH8PYpfF9Uv79HEacBncbHDMwfzZTSXc2VYttRgae1QAA9h5hAtUc8VhH1g")
    /** Create BlinkID recognizer */
    self.blinkIdRecognizer = MBBlinkIdCombinedRecognizer()
    self.blinkIdRecognizer?.returnFullDocumentImage = true;

    /** Create settings */
    let settings : MBBlinkIdOverlaySettings = MBBlinkIdOverlaySettings()

    /** Crate recognizer collection */
    let recognizerList = [self.blinkIdRecognizer!]
    let recognizerCollection : MBRecognizerCollection = MBRecognizerCollection(recognizers: recognizerList)

    /** Create your overlay view controller */
    let blinkIdOverlayViewController : MBBlinkIdOverlayViewController = MBBlinkIdOverlayViewController(settings: settings, recognizerCollection: recognizerCollection, delegate: self)

    /** Create recognizer view controller with wanted overlay view controller */
    let recognizerRunneViewController : UIViewController = MBViewControllerFactory.recognizerRunnerViewController(withOverlayViewController: blinkIdOverlayViewController)
    recognizerRunneViewController.modalPresentationStyle = .fullScreen

    /** Present the recognizer runner view controller. You can use other presentation methods as well (instead of presentViewController) */
    self.topMostController().present(recognizerRunneViewController, animated: true, completion: nil)

//    let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController
 //   navigationController?.pushViewController(recognizerRunneViewController, animated: true)


}

    func topMostController() -> UIViewController {
      var topController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
      while ((topController?.presentedViewController) != nil) {
        topController = topController?.presentedViewController
      }
      return topController!
    }
    func blinkIdOverlayViewControllerDidFinishScanning(_ blinkIdOverlayViewController: MBBlinkIdOverlayViewController, state: MBRecognizerResultState) {
        /** This is done on background thread */
        print("success scaning");          

    }

}

Upvotes: 1

Views: 602

Answers (1)

Cerovec
Cerovec

Reputation: 1313

You should remove the intermediate ViewController instance, and present the recognizerRunneViewController (which is responsible for ID Scanning) on top of either UINavigationViewController, or modally directly over the topMostViewController.

Here's a link to github issue which helped with the solution: https://github.com/BlinkID/blinkid-ios/issues/294


EDIT: BlinkID from June 2020. supports out of the box integration in Flutter apps with an official plugin. The plugin is available here: https://github.com/blinkid/blinkid-flutter

Upvotes: 3

Related Questions