2

I have a web application that I load in native app with WKWebView. The first navigation is login screen and I'm looking for a way to authenticate the user with ios bio-metrics. Please let me know if you can help me.

Ali Kashanchi
  • 8,620
  • 3
  • 14
  • 11

1 Answers1

0

Add a message handler to the WKWebView's user content controller. self.webView.configuration.userContentController.add(self, name: "AuthHandler")

Conform the class of self to WKScriptMessageHandler protocol and implement func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage).
In the web app, add a button that will call the message handler on clicking it:

<button id="authButton" onClick="callAuthHandler()"/>

func callAuthHandler(){
    window.webkit.messageHandlers.AuthHandler.postMessage("authCallback");
}
func authCallback(status){
   console.log(status);
}

In your native code,

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
   if message.name == "AuthHandler",let callBack = message.body as?
      String, LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil){
      LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "To authenticate") { (success, error) in
         self.webView.evaluateJavaScript(callBack+"(\(success))
      }
   }
}
Santhosh R
  • 1,518
  • 2
  • 10
  • 14
  • 1
    Thank you Santhosh. my only concern is anyone can log in to my system only by calling authCallBack() function and passing true. remember this is a public website. – Ali Kashanchi Dec 21 '17 at 23:37