Reputation: 359
I am having an issues where when I "close and open" an app, the same ViewController appears twice. The app is set up in such a way where when the user "closes and opens" the app, it will take them to a "PIN" ViewController where they input their PIN code. The problem is that when the user gets to the "PIN" ViewController, the ViewController shows up twice. How do I make it so that this condition doesn't occur? In addition, once the user enters the "PIN", how do I make it so that the user can go back to the page they were last one before "closing and then opening" the app?
Code in the App Delete to allow user to go to the "PIN" ViewController when they "open and close" the app:
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.
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
}
}
}
Upvotes: 0
Views: 697
Reputation: 100533
1- You should remove both of
DispatchQueue.global(qos: .background).async
DispatchQueue.main.async
as code is inside main thread by default
2- Code inside
applicationWillEnterForeground
will be called for example when you press home button and re-open the app again , and this isn't app close it's the app running in background
3- Use UINavigationController
to set back VC to return to it
let nav = ////
nav.viewControllers = [vc1,vc2]
appDelegate.window?.rootViewController = nav
so vc2 will show and with back you can return to vc1
Upvotes: 2