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