关闭并打开特定ViewController的iOS应用程序

关闭并打开特定ViewController的iOS应用程序,ios,swift,appdelegate,Ios,Swift,Appdelegate,我一直在寻找这个问题的答案,但似乎没有一个有效。我的问题是:当我在Swift中关闭然后重新打开一个应用程序时,我如何使它进入一个特定的ViewControlleri.e,一个PIN登录页面?我知道这与AppDelegate有关,但所有其他建议都以崩溃告终。以下是我的应用程序删除: class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application

我一直在寻找这个问题的答案,但似乎没有一个有效。我的问题是:当我在Swift中关闭然后重新打开一个应用程序时,我如何使它进入一个特定的ViewControlleri.e,一个PIN登录页面?我知道这与AppDelegate有关,但所有其他建议都以崩溃告终。以下是我的应用程序删除:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    return true
}

func applicationWillResignActive(_ application: UIApplication) {
}

func applicationDidEnterBackground(_ application: UIApplication) {

    let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
    let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    window?.inputViewController?.present(secondViewController, animated: true, completion: nil)
}

func applicationWillEnterForeground(_ application: UIApplication) {
}

func applicationDidBecomeActive(_ application: UIApplication) {
}

这里创建的扩展用于查找当前visiblecontroller

extension UIViewController {
    func topMostViewController() -> UIViewController {
        if self.presentedViewController == nil {
            return self
        }
        if let navigation = self.presentedViewController as? UINavigationController {
            return navigation.visibleViewController.topMostViewController()
        }
        if let tab = self.presentedViewController as? UITabBarController {
            if let selectedTab = tab.selectedViewController {
                return selectedTab.topMostViewController()
            }
            return tab.topMostViewController()
        }
        return self.presentedViewController!.topMostViewController()
    }
}

extension UIApplication {
    func topMostViewController() -> UIViewController? {
        return self.keyWindow?.rootViewController?.topMostViewController()
    }
}
之后,你可以随时使用它

let topMostViewController = UIApplication.shared.topMostViewController()

 let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    topMostViewController.present(secondViewController, animated: true, completion: nil)

这里创建的扩展用于查找当前visiblecontroller

extension UIViewController {
    func topMostViewController() -> UIViewController {
        if self.presentedViewController == nil {
            return self
        }
        if let navigation = self.presentedViewController as? UINavigationController {
            return navigation.visibleViewController.topMostViewController()
        }
        if let tab = self.presentedViewController as? UITabBarController {
            if let selectedTab = tab.selectedViewController {
                return selectedTab.topMostViewController()
            }
            return tab.topMostViewController()
        }
        return self.presentedViewController!.topMostViewController()
    }
}

extension UIApplication {
    func topMostViewController() -> UIViewController? {
        return self.keyWindow?.rootViewController?.topMostViewController()
    }
}
之后,你可以随时使用它

let topMostViewController = UIApplication.shared.topMostViewController()

 let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    topMostViewController.present(secondViewController, animated: true, completion: nil)

您应该将与演示文稿相关的代码放在applicationWillEnterForeground方法中,这将在用户下次单击appIcon并启动应用程序时调用

下面是使其工作的代码

func applicationWillEnterForeground(_ application: UIApplication) {

        let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
    let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    window?.rootViewController?.present(secondViewController, animated: true, completion: nil)
}

希望有帮助。

您应该将演示文稿相关代码放在applicationWillEnterForeground方法中,这将在用户下次单击appIcon并启动应用程序时调用

下面是使其工作的代码

func applicationWillEnterForeground(_ application: UIApplication) {

        let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
    let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    window?.rootViewController?.present(secondViewController, animated: true, completion: nil)
}

希望有帮助。

假设您正在将应用程序从后台移动到前台状态,您可以尝试以下操作:

  func applicationWillEnterForeground(_ application: UIApplication) {

       // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
       let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
       let pinViewConroller: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
       self.window?.rootViewController.present(pinViewConroller, animated: false, completion: nil)

    }

假设您正在将应用程序从后台移动到前台状态,您可以尝试以下操作:

  func applicationWillEnterForeground(_ application: UIApplication) {

       // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
       let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
       let pinViewConroller: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
       self.window?.rootViewController.present(pinViewConroller, animated: false, completion: nil)

    }

所以,当应用程序在终止后打开时,您需要始终打开特定的视图控制器?您必须找到当前的可见控制器,然后才能打开it@Himanth是,例如,当应用程序关闭并再次打开时,我想去一个控制器,让用户输入他们的PIN码,以继续类似于Credit Karma应用程序,所以在应用程序终止后打开时,您需要始终打开一个特定的视图控制器?您必须找到当前可见控制器,然后再打开it@Himanth是,例如,当应用程序关闭并再次打开时,我想去一个控制器,让用户输入他们的PIN码,以继续类似于信贷业力app@rengineer不用谢。请检查应答状态。可能对某人有帮助。谢谢。@renginer别客气。请检查应答状态。可能对某人有帮助。非常感谢。