如何在iOS 10中使用TouchID

前端之家收集整理的这篇文章主要介绍了如何在iOS 10中使用TouchID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的iOS应用程序中实现本地身份验证安全性,但我是
得到一个错误,不能弄清楚为什么我得到这个.

我正在使用iPhone 5s.这很重要吗

码:

  1. import UIKit
  2. import LocalAuthentication
  3.  
  4. class ViewController: UIViewController {
  5.  
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. // Do any additional setup after loading the view,typically from a nib.
  9. }
  10.  
  11. override func didReceiveMemoryWarning() {
  12. super.didReceiveMemoryWarning()
  13. // Dispose of any resources that can be recreated.
  14. }
  15.  
  16. @IBAction func action(_ sender: Any) {
  17. authenticateUser()
  18. }
  19.  
  20. func authenticateUser() {
  21. let authContext : LAContext = LAContext()
  22. var error: NSError?
  23.  
  24. if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,error: &error){
  25. authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,localizedReason: "Biometric Check for application",reply: {(successful: Bool,error: NSError?) -> Void in
  26. if successful{
  27. print("TouchID Yes")
  28. }
  29. else{
  30. print("TouchID No")
  31. }
  32. } as! (Bool,Error?) -> Void)
  33. }
  34. else{
  35. authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication,localizedReason: "Enter your Passcode",reply: {
  36. (successful: Bool,error: NSError?) in
  37. if successful{
  38. print("PassCode Yes")
  39. }
  40. else{
  41. print("PassCode No")
  42. }
  43. } as! (Bool,Error?) -> Void)
  44. }
  45. }
  46. }

错误

提前致谢.

解决方法

没有类型转换的代码应该可以工作
  1. func authenticateUser() {
  2. let authContext : LAContext = LAContext()
  3. var error: NSError?
  4.  
  5. if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,error: &error){
  6. authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,reply: {successful,error -> Void in
  7. if successful{
  8. print("TouchID Yes")
  9. }
  10. else{
  11. print("TouchID No")
  12. }
  13. }
  14. )
  15. }
  16. else{
  17. authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication,reply: {
  18. successful,error in
  19. if successful{
  20. print("PassCode Yes")
  21. }
  22. else{
  23. print("PassCode No")
  24. }
  25. }
  26. )
  27. }
  28. }

猜你在找的iOS相关文章