首页 > 解决方案 > 成功启用触摸ID后如何在下次打开应用程序时使用触摸ID登录

问题描述

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    IQKeyboardManager.shared.enable = true

    for family in UIFont.familyNames {
        print("\(family)")
        for name in UIFont.fontNames(forFamilyName: family) {
            print("   \(name)")
        }
    }
    if UserDefaults.standard.value(forKey: "userEmail") != nil {
        let RegisterUser = UserDefaults.standard.value(forKey: "RegisterUser") as? NSDictionary ?? [:]

        if RegisterUser.object(forKey: "AddAddress") as? String ?? "" == "0" {
            initialViewController  = Registration_iPohoneVC(nibName:"Registration_iPohoneVC",bundle:nil)
        }else if RegisterUser.object(forKey: "JobInfo") as? String ?? "" == "0" {
            initialViewController  = JobInfo_iPohoneVC(nibName:"JobInfo_iPohoneVC",bundle:nil)
        }else if RegisterUser.object(forKey: "UploadResume") as? String ?? "" == "0" {
            initialViewController  = Uploadresume_iPhoneVC1(nibName:"Uploadresume_iPhoneVC1",bundle:nil)
        }else if RegisterUser.object(forKey: "UploadVideo") as? String ?? "" == "0" {
            initialViewController  = Createvideo_iPhoneVC(nibName:"Createvideo_iPhoneVC",bundle:nil)
        }else {
            if UserDefaults.standard.value(forKey: "Touchid") as? Bool ?? false == true {
                let context = LAContext()
                var error: NSError?
                if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
                    let reason = "Touch the home button with your finger to LogIn."
                    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply:
                        {(succes, error) in
                            DispatchQueue.main.async {
                                if succes {
                                    if UserDefaults.standard.value(forKey: "Touchid") as? Bool ?? false == true {
                                        if UserDefaults.standard.value(forKey: "PlanSelected") as? Int ?? 0 == 90 {
                                            self.initialViewController  = Homescreen_iPhoneVC(nibName:"Homescreen_iPhoneVC",bundle:nil)
                                        }else {
                                            self.initialViewController  = Homescreen6_iPhoneVC(nibName:"Homescreen6_iPhoneVC",bundle:nil)
                                        }
                                    }
                                }else {
                                    let alertController = UIAlertController(title: "Touch ID Authentication Failed", message: "", preferredStyle: .alert)
                                    let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) {
                                        UIAlertAction in
                                        UserDefaults.standard.removeObject(forKey: "userEmail")
                                        UserDefaults.standard.removeObject(forKey: "userPassword")
                                        UserDefaults.standard.synchronize()
                                        self.initialViewController  = Login_iPhoneVC(nibName:"Login_iPhoneVC",bundle:nil)
                                        let frame = UIScreen.main.bounds
                                        self.window = UIWindow(frame: frame)
                                        self.window!.rootViewController = appdelegate!.initialViewController!
                                        self.window!.makeKeyAndVisible()
                                    }
                                    alertController.addAction(okAction)
                                    self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
                                }
                            }
                    })
                }else {
                    print("touch id not available...")
                }
            }else {
                if UserDefaults.standard.value(forKey: "PlanSelected") as? Int ?? 0 == 90 {
                    self.initialViewController  = Homescreen_iPhoneVC(nibName:"Homescreen_iPhoneVC",bundle:nil)
                }else {
                    self.initialViewController  = Homescreen6_iPhoneVC(nibName:"Homescreen6_iPhoneVC",bundle:nil)
                }
            }
        }
        let navigationcontroller = UINavigationController()
        navigationcontroller.viewControllers = [appdelegate!.initialViewController!]
        navigationcontroller.navigationBar.barTintColor = AppBG1Color
        navigationcontroller.interactivePopGestureRecognizer?.delegate = self as UIGestureRecognizerDelegate
        navigationcontroller.interactivePopGestureRecognizer?.isEnabled = true
        let frame = UIScreen.main.bounds
        window = UIWindow(frame: frame)
        window!.rootViewController = navigationcontroller
        window!.makeKeyAndVisible()
    }else {
        initialViewController = Login_iPhoneVC(nibName: "Login_iPhoneVC", bundle: nil)
        let frame = UIScreen.main.bounds
        window = UIWindow(frame: frame)
        window!.rootViewController = initialViewController
        window!.makeKeyAndVisible()
    }

    UINavigationBar.appearance().tintColor = .white
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard(_:)))
    tapGesture.cancelsTouchesInView = false
    window?.rootViewController?.view.addGestureRecognizer(tapGesture)

    return true
}

这是我的 Appdelegate 代码,在我的其他条件下,我希望在用户已经启用触摸 id 时首先弹出触摸 id 弹出窗口让用户询问触摸主页按钮登录,但它会直接崩溃,我希望用户已经启用触摸 id 然后他每次他再次打开应用程序时都可以通过触摸访问应用程序。如果任何人都可以提供帮助,那将是非常感谢的。

标签: swift

解决方案


如果他启用了 touch id 设置 bolean 标志为 UserDefaults,并且下次他运行应用程序时读取它,如果它是真的,那么让他使用 touch id,如果不是 - 需要问他。

PS您可能应该将所有这些逻辑移至 SceneDelegate :) AppDelegate 不再适合这些东西


推荐阅读