Reputation: 359
I've been looking on SO for the answer to this question but none seemed to work. My question is : when I close and then re-open an application in Swift, how do I make it so that it goes to a specific ViewController(i.e, to a PIN login page)? I know it has to do with App Delegate but all other suggestions ends in a crash. Below is my App Delete:
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) {
}
Upvotes: 1
Views: 262
Reputation: 404
func presentToVC(yourVC:UIViewController){
let navEditorViewController: UINavigationController = UINavigationController(rootViewController: yourVC)
navEditorViewController.isNavigationBarHidden = true
navEditorViewController.interactivePopGestureRecognizer?.delegate = self
(self.window?.rootViewController as! UINavigationController).visibleViewController?.present(navEditorViewController, animated: true, completion: nil)
}
Upvotes: 1
Reputation: 2040
Assuming that you are moving the app from background to foreground state, you can try this:
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)
}
Upvotes: 1
Reputation: 2916
You should put the presentation related code in applicationWillEnterForeground method, this will be invoked when next time user clicks on the appIcon and launch the app.
Here is the code to make it working.
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)
}
Hope it helps.
Upvotes: 1
Reputation: 3015
here created extension to find out current 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()
}
}
after that you can use it whenever you want
let topMostViewController = UIApplication.shared.topMostViewController()
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
topMostViewController.present(secondViewController, animated: true, completion: nil)
Upvotes: 1
Reputation: 1058
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
Upvotes: 1