【发布时间】:2016-10-03 13:13:54
【问题描述】:
我想启用 touchId 但我收到错误:
类型 'LAError' 没有成员 'UserCancel' "
我能找到它是LAError 的成员我错过了什么?
这就是 Xcode 给我的。
类型“LAError”没有成员“AuthenticationFailed”
类型“LAError”没有成员“UserCancel”
类型“LAError”没有成员“UserFallback”
这是我的代码:
func authenticateUser()
{
let context = LAContext()
var error: NSError?
let reasonString = "Authentication is needed to access your app! :)"
if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error)
{
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (
success, Error) -> Void in
if success
{
print("Authentication successful! :) ")
}
else
{
switch(error!._code) {
case LAError.AuthenticationFailed.rawValue:
print("Authentication was cancelled by the system.")
case LAError.UserCancel.rawValue:
print("Authentication was cancelled by the user.")
case LAError.UserFallback.rawValue:
print("User selected to enter password.")
OperationQueue.main.addOperation({ () -> Void in
self.showPasswordAlert()
})
default:
print("Authentication failed! :(")
OperationQueue.main.addOperation({ () -> Void in
self.showPasswordAlert()
})
}
}
})
}
else
{
print(error?.localizedDescription)
OperationQueue.main.addOperation({ () -> Void in
self.showPasswordAlert()
})
}
}
// MARK: Password Alert
func showPasswordAlert()
{
let alertController = UIAlertController(title: "Touch ID Password", message: "Please enter your password.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel) { (action) -> Void in
if let textField = alertController.textFields?.first as UITextField?
{
if textField.text == "veasoftware"
{
print("Authentication successful! :) ")
}
else
{
self.showPasswordAlert()
}
}
}
alertController.addAction(defaultAction)
alertController.addTextField { (textField) -> Void in
textField.placeholder = "Password"
//textField.secureTextEntry = true
}
self.present(alertController, animated: true, completion: nil)
}
【问题讨论】:
-
您查找文档了吗?根据developer.apple.com/reference/localauthentication/laerror.code,错误代码以小写字母开头...
-
非常感谢