-1

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

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
sagarchavan
  • 187
  • 3
  • 14

3 Answers3

1

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()
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • Actually I am checking remember me is true or not in didFinishLaunchingWithOptions() of AppDelegate and accordingly I used to open the specific controller. So in the same method only I am checking user enabled touch id or not, if user authenticate for touch id then i am showing popup else redirecting normally to dashboard. Is it proper approach? And one more thing i wants to ask is, when pause the app clicking home button and if i wants to show touch id again when app reopens where to call that authentication method. Will it go to applicationWillEnterForeground()? – sagarchavan May 07 '19 at 06:52
  • Great answer. It is working fine now. But the dashboard content is getting visible in background when Touch ID opens with applicationWillEnterForeground() – sagarchavan May 07 '19 at 09:23
0

Save if your user has already logged in, in your UserDefaults and proceed with the app launch as follows:

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
}

where isLoggedIn bool should be your stored value from your UserDefaults.

0

You've to store true in userDefault where user is successFully logIn

e.g. UserDefaults.standard.setValue("true", forKey: "isLogin")

In 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
    }

And also create a method in 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()
    }

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31