我想在我的iOS应用中实现本地身份验证安全性,但我是 得到错误而无法弄清楚我为什么会这样做。
我正在使用iPhone 5s。这有关系吗?
代码:
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func action(_ sender: Any) {
authenticateUser()
}
func authenticateUser() {
let authContext : LAContext = LAContext()
var error: NSError?
if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {(successful: Bool, error: NSError?) -> Void in
if successful{
print("TouchID Yes")
}
else{
print("TouchID No")
}
} as! (Bool, Error?) -> Void)
}
else{
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: {
(successful: Bool, error: NSError?) in
if successful{
print("PassCode Yes")
}
else{
print("PassCode No")
}
} as! (Bool, Error?) -> Void)
}
}
}
错误:
提前致谢。
答案 0 :(得分:5)
没有类型转换的代码应该可以正常工作
func authenticateUser() {
let authContext : LAContext = LAContext()
var error: NSError?
if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {successful, error -> Void in
if successful{
print("TouchID Yes")
}
else{
print("TouchID No")
}
}
)
}
else{
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: {
successful,error in
if successful{
print("PassCode Yes")
}
else{
print("PassCode No")
}
}
)
}
}