Dermot
Dermot

Reputation: 1743

How to detect when App moves to/from Suspended state?

Is there an event that is fired, or any way of telling when my app moves in or out of the suspended state?

EDIT: I have read all documents related to AppDelegate events, and im aware there isn't any way, from that class to detect moving to suspended state.

Specifically, if my app is playing music in the background, and the music is paused, such as via a remote control, the music, and shortly afterwards all execution, suspends. When the user presses play again via the remote (or lock screen for example), the music starts again and execution continues (the app moves from suspended state to background state). Is there any way I can detect this? I need to close some services such as disconnect network sockets, stop bonjour services etc...

Upvotes: 2

Views: 3367

Answers (2)

Dermot
Dermot

Reputation: 1743

I've ended up using a combination of the app resigned/became active and (as my app plays audio in the background) the Begin and End AudioInterruption events of the AudioSession object, along with the callback of the Task Completion handler to decide if my app is about to be suspended.

For example (pseudocode):

if(BeginAudioInterruption && AppIsInBackground)
   WillSuspend();
else if(AppWillResignActive && MusicPlaying == false)
   WillSuspend();
else if(AppWillResignActive && TaskCompletion.TimeRemainingSeconds < 5 && MusicPlaying == false)
  WillSuspend();
else // etc...

Upvotes: 2

Piyush Kashyap
Piyush Kashyap

Reputation: 1965

Take a look at this image ,It shows complete life cycle of app

http://www.cocoanetics.com/files/UIApplication_Delegate_post_4_v121.jpg

Upvotes: 5

Related Questions