--from 《iPhone Application Development For Dummies - Neal Goldstein》
Various events besides termination can interrupt your application to allowthe user to respond — for example, incoming phone calls, SMS messages,calendar alerts, or the user pressing the Sleep button on an iPhone. Suchinterruptions may only be temporary. If the user chooses to ignore an inter-ruption, your application continues running as before. If the user decides toanswer the phone or reply to an SMS message, however, your application willbe terminated.
Figure 6-8 shows the sequence of events that occurs during the arrival of aphone call, SMS message, or calendar alert. Here’s what that looks like stepby step:
1. The system detects an incoming phone call or SMS message, or a cal-endar event occurs.
2. The system sends your application delegate theapplicationWillResignActive:message.
Because these interruptions cause a temporary loss of control by yourapplication — meaning that touch events are no longer sent to yourapplications — it is up to you to prevent what is known in the trade asa “negative user experience.” For example, if your application is a game,you should pause the game.
-
The system displays an alert panel with information about the event.
The user can choose to ignore the event or respond to it.
-
If the user ignores the event, the system sends your application del-egate theapplicationDidBecomeActive:message and resumes thedelivery of touch events to your application.
You can use this delegate method to restore the application to the stateit was in before the interruption. What you do depends on your applica-tion. In some applications, it makes sense to resume normal processing.In others — if you’ve paused a game, for example — you could leave thegame paused until the user decides to resume play.
-
If the user responds to the event, instead of ignoring it, the systemsends your application delegate theapplicationWillTerminate:message.
Your application should do what it needs to do in order to terminategracefully.
The way the Sleep/Wake button is handled is a little different. When theapplication enters or resumes from a sleep state, two messages are sent tothe application delegate:applicationWillResignActive:andapplica-tionDidBecomeActive:, respectively. In this case, your application alwaysresumes, though the user might immediately launch a different application.