Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onResume/onPause on iOS application

Tags:

ios

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.

like image 385
n179911 Avatar asked Feb 11 '15 01:02

n179911


1 Answers

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.

like image 143
Pixel Avatar answered Sep 23 '22 03:09

Pixel