-1

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

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

更新:1

我正在检查记住我是不是真的didFinishLaunchingWithOptions()AppDelegate因此,我曾经打开特定的UIViewController. 因此,在相同的方法中,我只检查用户是否启用了触摸 ID,如果用户对触摸 ID 进行身份验证,那么我将显示弹出窗口,否则将正常重定向到仪表板。这是一个正确的方法吗?我想问的另一件事是,当暂停应用程序单击主页按钮时,如果我想在应用程序重新打开时再次显示触摸 ID,则调用该身份验证方法。会去applicationWillEnterForeground()吗?

更新:2

当 Touch ID 打开时,仪表板内容在后台可见applicationWillEnterForeground()

4

3 回答 3

1

根据我的经验,您需要将 authentication相关代码和其他UIViewController代码分开。我建议singleton为 Bio- authentication matric(TouchID 和 FaceID)创建一个基于块的类

请参阅基于块的身份验证库BiometricAuthentication供您参考。

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

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

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总是在登录屏幕。只需设置您的初始控制器登录屏幕 storyboard

更新:1

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

是的,这是一种正确的方法,但请记住生物矩阵认证的代码集中化。

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

如果应用程序状态已更改,您可以使用applicationWillEnterForegroundOR 。applicationDidBecomeActive还有一件事,我想在上面提到,当用户重新打开应用程序时,也会调用这两种方法。如果您想完全限制用户访问应用程序内容,请使用,applicationWillEnterForeground()否则您可以使用applicationDidBecomeActive

更新:2

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

代码:

let blurEffect = UIBlurEffect(style: .Light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = view.bounds
self.view.addSubview(blurVisualEffectView)

认证成功则移除

blurVisualEffectView.removeFromSuperview()
于 2019-05-06T13:25:27.610 回答
0

如果您的用户已经登录,请保存在您的 UserDefaults 中并继续应用程序启动,如下所示:

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 应该是您在 UserDefaults 中存储的值。

于 2019-05-06T12:39:31.390 回答
0

您必须存储trueuserDefault用户成功的位置完全登录

例如 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
    }

并且还创建了method一个AppDelegate.swift

   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()
    }

于 2019-05-06T12:49:11.493 回答