Search code examples
iosxcodeswiftuiapplication

How to present Lock_ViewController when app is entering foreground in SWIFT?


Please help me. I have an app that has a passcode lock feature, this gets activated everytime the user opens the app. If the user goes to home ( app goes to background) and then go back to the app ( foreground) I want to display the lock screen again ( or use other viewcontroller ). How am I going to do this?

I know about the WillEnterForeground in appDelegate, I just can't figure out how am I going to tell my app to present the lock screen instead of the screen it had prior to going to background.

This app is very similar to the photo vault application. Photos are guarded with lock screen and user gets asked the passcode everytime he opens the app

Thank you so much in advance


Solution

  • There are a couple of libraries available

    https://github.com/antiraum/THPinViewController

    https://github.com/D-32/DMPasscode

    https://github.com/tiny2n/JKLLockScreenViewController

    If you wish to do it yourself or in middle of implenting it, get the top most controller in willEnterForground method, present your lock screen controller on that controller, on successful pin checking dismiss your lock screen controller, and if you need to notify others, push a notificiation This is how you can get the top most controller

    - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
         if ([rootViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController* tabBarController = (UITabBarController*)rootViewController;
            return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
         } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navigationController = (UINavigationController*)rootViewController;
            return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
         } else if (rootViewController.presentedViewController) {
            UIViewController* presentedViewController = rootViewController.presentedViewController;
            return [self topViewControllerWithRootViewController:presentedViewController];
         } else {
            return rootViewController;
        }
    }
    

    Swift

    func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController! {
    
        if rootViewController.isKindOfClass(UITabBarController) {
            let tabbarController =  rootViewController as! UITabBarController
            return self.topViewControllerWithRootViewController(tabbarController.selectedViewController)
        }else if (rootViewController.isKindOfClass(UINavigationController)) {
            let navigationController = rootViewController as! UINavigationController
            return self.topViewControllerWithRootViewController(navigationController.visibleViewController)
        }else if ((rootViewController.presentedViewController) != nil){
            let controller = rootViewController.presentedViewController
            return self.topViewControllerWithRootViewController(controller)
        }else {
            return rootViewController
        }
    
    }
    

    Usage

        let topController = self.topViewControllerWithRootViewController(UIApplication.sharedApplication().delegate?.window??.rootViewController)
    

    Modifiy and make an extension or use it as you wish