Presenting a specific view controller from AppDelegate

前端 未结 4 1359
时光说笑
时光说笑 2021-01-05 02:50

I am trying to present a view controller (a passcode request type view) every time my app becomes active. Once the correct passcode is entered, it should pop off the stack.

相关标签:
4条回答
  • 2021-01-05 03:28

    Here is complete Solution in Swift 4 implement this in didFinishLaunchingWithOptions

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
     let isLogin = UserDefaults.standard.bool(forKey: "Islogin")
        if isLogin{
            self.NextViewController(storybordid: "OtherViewController")
    
    
        }else{
            self.NextViewController(storybordid: "LoginViewController")
    
        }
    }
    

    write this Function any where inside Appdelegate.swift

      func NextViewController(storybordid:String)
    {
    
        let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let exampleVC = storyBoard.instantiateViewController(withIdentifier:storybordid )
       // self.present(exampleVC, animated: true)
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = exampleVC
        self.window?.makeKeyAndVisible()
    }
    
    0 讨论(0)
  • 2021-01-05 03:37

    Swift version of Quick and generalised way:

    getRootViewController().presentViewController(messageVC, animated: true, completion: nil)
    
    func getRootViewController() -> UIViewController
    {
        return (UIApplication.sharedApplication().delegate?.window??.rootViewController)!
    }
    
    0 讨论(0)
  • 2021-01-05 03:45

    I suggest you use applicationWillEnterForeground:, not applicationDidBecomeActive:, because it works better with multitasking gestures. For example, you don't want to put up the lock screen if the user double-clicks the home button to display the task bar, and then dismisses the task bar without changing apps.

    Presumably your AppDelegate has a window property, and you know that your window's root view controller is a UINavigationController.

    - (void)applicationWillEnterForeground:(UIApplication *)application {
        PasscodeViewController *pvc = [[PasscodeViewController alloc] init];
        [(UINavigationController *)self.window.rootViewController pushViewController:pvc animated:NO];
        // [pvc release]; if not using ARC
    }
    
    0 讨论(0)
  • 2021-01-05 03:48

    Solved

    PasscodeViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"passcodeVCID"]; 
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:vc];
    navController.navigationBar.hidden = YES;
    vc.mode = @"enable";
    self.window.rootViewController = navController;  
    

    Using in applicationWillEnterForeground method.

    0 讨论(0)
提交回复
热议问题