如何利用身份验证与生物识别技术在Web视图中对用户进行身份验证?

时间:2017-12-20 22:33:14

标签: ios swift swift4 biometrics lacontext

我有一个Web应用程序,我使用WKWebView加载到本机应用程序中。第一个导航是登录屏幕,我正在寻找一种使用ios bio-metrics来验证用户的方法。如果你能帮助我,请告诉我。

1 个答案:

答案 0 :(得分:0)

WKWebView的用户内容控制器添加消息处理程序。 self.webView.configuration.userContentController.add(self, name: "AuthHandler")

self类转换为WKScriptMessageHandler协议并实施func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
在Web应用程序中,添加一个按钮,在单击它时将调用消息处理程序:

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

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

在您的原生代码中,

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