On Android, when I write an application, I need to implement onResume/onPause for my activity to go to background and come back from foreground. I have googled if I need how to to the same thing for iOS. But I can't find anything useful. Can you please tell me what do I need to do to handle my iOS app going to background and coming back to foreground?
Thank you.
In Swift you can use
NotificationCenter.default.addObserver(self, selector: #selector(onPause), name:
UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(onResume), name:
UIApplication.willEnterForegroundNotification, object: nil)
with
@objc func onPause() {
}
@objc func onResume() {
}
Note that in practice I found replacing UIApplication.willEnterForegroundNotification
with UIApplication.didBecomeActiveNotification
worked better because onResume
is then also triggered if you click the Home button twice to open the iOS 'open apps' screen, then click the app immediately to focus it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With