How should I handle an incoming call when my application is active? Will my application terminate or pause?
views:
246answers:
3Hello young adventurer. Your decision to take up the life of an iPhone Developer will test you, but the rewards can be great. For the answer to your question I will need you to perform a series of tasks for me. The first task is to clear my basement from a rat infestation. You can find the door to the basement just behind the curtains by the bar. If you see any kobolds you may slay them as well. When you have eight rat tails come back to see me for your reward.
To answer your question it kind of depends on what you are trying to do, but in short applicationWillResignActive
is called when there is an incoming call and then your app is kind of disabled. willApplicationTerminate
will be called if the user chooses to answer the call. For more information have a closer look at Responding to Interruptions Guide
In short if your app is a game it would be wise to pause it ASAP so do that in applicationWillResignActive
for less critical needs and willApplicationTerminate
is where you save state before closing down for good.
I believe the app will terminate when an incoming call is received, but will restart after the call is finished. (Although I could be mistaken on this point, and the app may become "inactive" and "active" again after the call finishes. I haven't checked this out 100%).
If the app terminates, you'll need to respond to the applicationWillTerminate
delegate method in your application delegate class.
If the app only becomes inactive, then you'll need to respond to the application[Did/Will]BecomeInactive
and application[Did/Will]BecomeActive
methods in your application delegate.
Upon receiving a call, the system will call your app delegate's applicationWillResignActive: method and show the option to receive the incoming call or not, that's the standard behavior at least until iOS4, but after that we have 3 possible outcomes:
1) The user ignores the call:
-> then it's called the app delegate's applicationWillBecomeActive: method.
2) the user decides to answer the call (until iOS 3.x):
-> your app delegate's applicationWillTerminate: is called, and you should save any needed data, free used memory and all that stuff.
3) the user decides to answer the call (iOS 4):
-> the app delegate’s applicationDidEnterBackground: method is called, and you should treat this, too, but it's not as terrible as applicationWillTerminate, just be sure to save sensible data and dealloc as much memory as possible.
I know it's been a while, but I've also had to search for this, and I'm answering as well as possible just for those who may eventually need this from now on :)
A good place to read this in more detail is apple's own Application Programming Guide: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/CoreApplication/CoreApplication.html#//apple_ref/doc/uid/TP40007072-CH3-SW9