applicationDidEnterBackground and applicationWillEnterForeground method are not called when pressed home button in iOS simulator

On iOS13+, if you implement UIWindowSceneDelegate, it calls func sceneDidEnterBackground(_ scene: UIScene), instead.


The reason why applicationDidEnterBackground: and applicationDidEnterForeground: are never called is because these methods are used in joint with Application does not run in background this option can be found in your ***-info.plist. If this option is set to YES than your app will never call these methods, because these when you press the home button with an app that has set the option to YES the instance of the app that is running will get terminated so everytime you press the home button and then select the app icon a new instance is being created so it is using applicationWillTerminate:.

The methods that Kirti mali has said would also be the incorrect methods to use for want you are after, the reason being is that applicationDidBecomeActive: and applicationWillResignActive: are used when something like when you answer a phone call. The instance running is not terminated neither is it sent to the background. The instance is paused until the user has finished on that call when it will become active again.

So the solution to this would be if you want the app to run in background would be to change the option "Application does not run in background" in the ***-info.plist to beNOjustapplicationDidBecomeActive:andapplicationWillResignActive:` is the wrong way for these methods to be used.

Please see the apple documentation on UIApplicationDelegate to get a better understanding of these methods.


Since your App runs on background those methods will never called however you can use willEnterForegroundNotification and didEnterBackgroundNotification to do same thing you want.

You can write following codes inside the didFinishLaunchingWithOptions method of ApplicationDelegate

    NotificationCenter.default.addObserver(forName:UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { (_) in
        // Your Code here
}

NotificationCenter.default.addObserver(forName:UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { (_) in
       // Your Code here        
}