Ad
Remove Previous View Controllers From View Hierarchy After Update From Background
I want to remove previous view controllers from view hierarchy after update my app from background. To achieve this, I set background fetch completion handler.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(1200)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// reload occur
reloadApp()
completionHandler(.newData)
}
For reloading, I instantiate
ViewController from StoryBoard.
func reloadApp() {
// previous ViewController Stack has not been removed.
let rootStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = rootStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController")
rootVC.view.layoutIfNeeded()
UIApplication.shared.keyWindow?.rootViewController = rootVC
UIApplication.shared.keyWindow?.makeKeyAndVisible()
}
This approach reloads new ViewController correctly. But, I still have previous view controllers. Below image shows my situation.
Any ideas about this problem?
Ad
Answer
if let therootController = UIApplication.shared.keyWindow?.rootViewController {
// If rootViewcontroller is navigationController then pop to root if any controllers has been pushed, dismiss if any controllers has been presented.
}
UIApplication.shared.keyWindow?.rootViewController = nil
let rootStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = rootStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController")
rootVC.view.layoutIfNeeded()
UIApplication.shared.keyWindow?.rootViewController = rootVC
UIApplication.shared.keyWindow?.makeKeyAndVisible()
Ad
source: stackoverflow.com
Related Questions
- → Function Undefined in Axios promise
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Click to navigate on mobile devices
- → Playing Video - Server is not correctly configured - 12939
- → How to allow api access to android or ios app only(laravel)?
- → Axios array map callback
- → Access the Camera and CameraRoll on Android using React Native?
- → Update React [Native] View on Day Change
- → Shopify iOS SDK - issue converting BuyProductVariant to BuyProduct
- → BigCommerce and shopify API
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → React Native - Differences between Android and IOS
- → What is the difference between React Native and React?
Ad