这是uiswitch valueChange action
@IBAction func swichTouchIdState(sender: UISwitch) {
print("newState:\(sender.on)")
if sender.on == false {
AuthenticationSetter.touchIdAuthentication() {
isSuccess, error in
if isSuccess == true {
AuthenticationSetter.setAuthenticationSwitchUserDefault(false)
} else {
sender.setOn(true, animated: true)
}
if let laError = error as? LAError {
print("error happened in authenticating: \(laError)")
switch laError {
case .AppCancel:
print("App Cancel")
self.touchIdSwitch.setOn(true, animated: true)
break
case .SystemCancel:
print("System Cancel")
self.touchIdSwitch.setOn(true, animated: true)
break
case .UserCancel:
print("User Cancel")
dispatch_async(dispatch_get_main_queue()) {
self.touchIdSwitch.setOn(true, animated: false)
}
break
default:
break
}
} else {
print("no error")
}
}
} else {
AuthenticationSetter.setAuthenticationSwitchUserDefault(true)
}
}
这里是AuthenticationSetter.touchIdAuthentication函数
class func touchIdAuthentication(doAfterAuthentication: (Bool, NSError?) -> Void) {
let laContext = LAContext()
var error: NSError?
if laContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
laContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "TouchID Authentication", reply: doAfterAuthentication)
} else {
print("device don't support touchId")
}
}
当我在touchid窗口触摸取消时,会打印“用户取消”,但开关没有打开。
我创建了一个新的demoViewController,它有一个UISwitch和一个按钮
当调用按下此函数时:demoSwitch.setOn(demoSwitch.on, true)
它运行得很好!!!!任何ios大师都可以帮助我!