iosswiftlocalauthentication

From where to call Touch ID logic?


I am a newbie to iOS. I am trying a local authentication framework in the app. My app flow is like when a user opens App he can able to see the splash screen, then if he is a new user he will redirect to the login screen and then to the dashboard screen. From login, if he clicks remembered me on, next time when the user opens the app he will directly redirect to Dashboard.

I just don't understand on which screen I add authenticationWithTouchID logic. On app open, I want to show TouchID popup so that the user can authenticate and redirect to the dashboard.

Update:1

I am checking remember me is true or not in didFinishLaunchingWithOptions() of AppDelegate and accordingly, I used to open the specific UIViewController. So in the same method only I am checking user enabled touch id or not, if the user authenticates for touch id then I am showing popup else redirecting normally to dashboard. Is it a proper approach? And one more thing I want to ask is when pausing the app clicking home button and if I want to show touch id again when app reopens were to call that authentication method. Will it go to applicationWillEnterForeground()?

Update:2

The dashboard content is getting visible in the background when Touch ID opens with applicationWillEnterForeground()


Solution

  • Based on my experience, You need to separate both authentication related and other UIViewController code. I suggest create a block-based singleton class for Bio-matric authentication (TouchID and FaceID)

    Refer awsome block-based authentication library BiometricAuthentication for your reference.

    I suggest keep all authentication related code into the Login screen.

    Refer below code for auto 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")
                }
            }
        }
    }
    

    If you go with this approach then no need to write extra code in AppDelegate.swift file because of your rootViewController always the Login screen. Just set your initial controller Login screen from storyboard

    Update:1

    Question: Is it a proper approach?

    Yes, It is a proper way to do this, But code centralization for Bio-matric authentication keep in mind.

    Question: How can I Managed TouchID OR FaceID Management if application state changed

    You can go with applicationWillEnterForeground OR applicationDidBecomeActive if application state was changed. One more thing, I would like to mention above both methods are also called when the user open app freshly. If you want completely restrict the user to access the app content then go with applicationWillEnterForeground() otherwise you can go with applicationDidBecomeActive

    Update:2

    You need to add dummy blur UIView manually if you want to restrict app content.

    Code:

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

    Remove if authenticate successfully

    blurVisualEffectView.removeFromSuperview()