Ios 从何处调用触摸ID逻辑?

Ios 从何处调用触摸ID逻辑?,ios,swift,localauthentication,Ios,Swift,Localauthentication,我是iOS的新手。我正在应用程序中尝试本地身份验证框架。我的应用程序流程就像当用户打开应用程序时,他可以看到启动屏幕,然后如果他是新用户,他将重定向到登录屏幕,然后重定向到仪表板屏幕。在登录时,如果他单击Membered me on,下次用户打开应用程序时,他将直接重定向到Dashboard 我只是不明白我在哪个屏幕上添加了带有TouchID逻辑的身份验证。在应用程序打开时,我想显示TouchID弹出窗口,以便用户可以进行身份验证并重定向到仪表板 更新:1 我在AppDelegate的didFi

我是iOS的新手。我正在应用程序中尝试本地身份验证框架。我的应用程序流程就像当用户打开应用程序时,他可以看到启动屏幕,然后如果他是新用户,他将重定向到登录屏幕,然后重定向到仪表板屏幕。在登录时,如果他单击Membered me on,下次用户打开应用程序时,他将直接重定向到Dashboard

我只是不明白我在哪个屏幕上添加了带有TouchID逻辑的身份验证。在应用程序打开时,我想显示TouchID弹出窗口,以便用户可以进行身份验证并重定向到仪表板

更新:1

我在
AppDelegate
didFinishLaunchingWithOptions()
中检查Memory me是否为真,因此,我用来打开特定的
UIViewController
。因此,在相同的方法中,我只检查用户启用的触摸id是否正确,如果用户验证了触摸id,那么我将显示弹出窗口,否则将正常重定向到仪表板。这是一个正确的方法吗?我想问的另一件事是,当暂停应用程序时,单击home(主页)按钮,如果我想在应用程序重新打开时再次显示touch id,我将调用该身份验证方法。它是否会转到
应用程序willenterforeground()

更新:2


当Touch ID以
applicationWillEnterForeground()

打开时,仪表板内容将在后台可见。如果您的用户已登录,请在您的用户默认设置中保存,然后按以下步骤启动应用程序:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    self.window = UIWindow(frame: UIScreen.main.bounds)
    if !isLoggedIn {
        let loginController = LoginController()
        self.window?.rootViewController = loginController
        self.window?.makeKeyAndVisible()
        return true
    }
    let authController = AuthenticaeController()
    self.window?.rootViewController = authController
    self.window?.makeKeyAndVisible()

    return true
}

其中isLoggedIn bool应该是用户默认值中的存储值。

您必须将
true
存储在用户成功登录的
userDefault

e、 g。
UserDefaults.standard.setValue(“true”,forKey:“isLogin”)

AppDelegate.Swift中

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

        let islogin = UserDefaults.standard.bool(forKey: "isLogin")

        if islogin
        {
           self.NextViewController(storybordid: "DashBoardViewIdentifier")
        }
        else
        {
            self.NextViewController(storybordid: "LoginViewIdentifier")
        }
        return true
    }
   func NextViewController(storybordid:String)
    {

        let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let exampleVC = storyBoard.instantiateViewController(withIdentifier:storybordid )
        let nav = UINavigationController(rootViewController: exampleVC)
        nav.navigationController?.setNavigationBarHidden(true, animated: false)
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = nav
        self.window?.makeKeyAndVisible()
    }

并在
AppDelegate.swift

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

        let islogin = UserDefaults.standard.bool(forKey: "isLogin")

        if islogin
        {
           self.NextViewController(storybordid: "DashBoardViewIdentifier")
        }
        else
        {
            self.NextViewController(storybordid: "LoginViewIdentifier")
        }
        return true
    }
   func NextViewController(storybordid:String)
    {

        let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let exampleVC = storyBoard.instantiateViewController(withIdentifier:storybordid )
        let nav = UINavigationController(rootViewController: exampleVC)
        nav.navigationController?.setNavigationBarHidden(true, animated: false)
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = nav
        self.window?.makeKeyAndVisible()
    }

根据我的经验,你需要把两者分开
authentication
相关和其他
UIViewController
code。我建议 为Bio matric
身份验证创建基于块的
单例
类
(TouchID和FaceID)

请参阅一些基于块的身份验证库以供参考

我建议将所有与身份验证相关的代码保留在
登录
屏幕中。

请参阅下面的自动登录代码

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if isRemmberMe{
        BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in

            switch result {
            case .success( _):
                print("Redirect into dashboard screen")
            case .failure(let error):
                print("Authentication Failed")
            }
        }
    }
}
如果采用这种方法,则无需在中编写额外的代码
AppDelegate.swift
文件,因为您的
rootViewController
始终是 登录屏幕。只需从设置初始控制器登录屏幕
情节提要

更新:1

问题:这是正确的方法吗?

是的,这是一种正确的方法,但是对于 记住生物矩阵认证

问题:如果应用程序状态更改,我如何管理TouchID或FaceID管理

您可以使用
应用程序进入前台
applicationIDBECOMEACTIVE
如果应用程序状态已更改。一个 更重要的是,我想提到以上两种方法也被称为 当用户打开应用程序时。如果您想完全限制用户访问应用程序内容,请使用
applicationWillEnterForeground()
否则您可以使用
applicationdidebecomeactive

更新:2

如果要限制应用程序内容,需要手动添加虚拟模糊
UIView

代码:

let blurEffect = UIBlurEffect(style: .Light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = view.bounds
self.view.addSubview(blurVisualEffectView)
如果验证成功,则删除

blurVisualEffectView.removeFromSuperview()

我想这会出现在登录屏幕上。实际上,我在AppDelegate的didFinishLaunchingWithOptions()中检查MemberMe是否为真,因此我使用它打开特定的控制器。因此,在相同的方法中,我只检查用户启用的触摸id是否正确,如果用户对触摸id进行身份验证,那么我将显示弹出窗口,否则将正常重定向到仪表板。这是正确的方法吗?我想问的另一件事是,当暂停应用程序时,单击home按钮,以及当应用程序重新打开时,我是否想再次显示touch id,在哪里调用该身份验证方法。它是否会进入应用程序willenterforeground()?回答得很好。它现在运转良好。但当使用applicationWillEnterForeground()打开Touch ID时,仪表板内容在后台变得可见