我正在尝试添加一个TouchID设置,允许用户通过TouchID进行身份验证,如果该选项已启用,但我似乎无法让它工作。经过一些测试,应用程序中断。它无法在Simulator中执行,以下代码以红色突出显示:
收到此错误后,即使撤消每个更改,它仍会显示,我必须使用整个项目文件夹的备份。
我处理Touch ID身份验证的代码:
if NSUserDefaults.standardUserDefaults().boolForKey("TouchID"){
let myContext = LAContext()
var authError: NSError?
// Set the reason string that will appear on the authentication alert.
let myReasonString = "String explaining why app needs authentication"
if myContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
[myContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: myReasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in
if success {
// User authenticated successfully
} else {
// Authenticate failed
}
})]
} else{
// Could not evaluate policy; check authError
}
}
处理交换机的页面部分:
import UIKit
class TableViewController: UITableViewController {
@IBOutlet weak var TouchID: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
self.restoreSwitchesStates();
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TableViewController.saveSwitchesStates), name: "kSaveSwitchesStatesNotification", object: nil);
}
func saveSwitchesStates() {
NSUserDefaults.standardUserDefaults().setBool(TouchID!.on, forKey: "TouchID");
NSUserDefaults.standardUserDefaults().synchronize();
}
func restoreSwitchesStates() {
TouchID!.on = NSUserDefaults.standardUserDefaults().boolForKey("TouchID");
}