ViewDidAppear is not called when opening the app from the background


In Swift, the viewDidAppear method does not call when an application is opened from the background. In that case, you have to use the applicationWillEnterForeground method in the AppDelegate. In this article, you will see an example of how to perform an action when opening an app from the background.

Approach

If you open an app from the background, it should call the applicationWillEnterForeground method of the app delegate before presenting the view. However, it's possible that the viewDidAppear method of the view controller is not called if the view controller's view is already on the screen or if the app has not been completely terminated.

If you want to ensure that viewDidAppear is called every time a view controller's view appears, you can put the code you want to execute in that method in a separate method and call it from both viewDidAppear and applicationWillEnterForeground. This way, your code will execute both when the view controller's view appears on the screen and when the app returns from the background.

Example

Sure, here's an example of how to implement the approach −

import UIKit
class TestController: UIViewController {
    
   override func viewDidLoad() {
      super.viewDidLoad()
      initialSetup()
   }
    
   override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
   }
    
   private func initialSetup() {
        
      view.backgroundColor = .white
      navigationItem.title = "Background Call"
        
      NotificationCenter.default.addObserver(self, selector: #selector(refreshData), name: .init("RefreshData"), object: nil)
   }
    
   @objc private func refreshData() {
      print("You need to refresh the data...")
        
      // Write code here to refresh the data
   }
}

Once you run the above code, you will see that the viewDidAppear method will not call when you open the app from the background. In order to execute, you can implement the applicationWillEnterForeground in the AppDelegate like below −

import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
   var window: UIWindow?
    
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
      // write code here to set initial view controller programatically
      self.window = UIWindow(frame: UIScreen.main.bounds)
      let navigation = UINavigationController(rootViewController: TestController())
      self.window?.rootViewController = navigation
      self.window?.makeKeyAndVisible()
        
      return true
   }
    
   func applicationWillEnterForeground(_ application: UIApplication) {
      NotificationCenter.default.post(name: .init("RefreshData"), object: nil)
   }
}

This way, the refreshData() method will be called both when the view controller's view appears on the screen and when the app returns from the background.

Note that applicationWillEnterForeground is a method of the app delegate, not the view controller, so you'll need to implement it in your app delegate and call the appropriate method on your view controller instance.

Conclusion

In conclusion, the viewDidAppear method of a view controller in iOS is not always called when the app returns from the background. To ensure that your code is executed both when the app returns from the background and when a view controller's view appears on the screen, you can define a separate method to hold the code you want to execute and call that method from both viewDidAppear and applicationWillEnterForeground. This approach can be useful for refreshing data, tracking analytics, or handling push notifications, among other use cases.

Updated on: 04-May-2023

828 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements