Reacting to iPhone Status Changes Events

By Blue_Chi | Flash CS5 | ActionScript 3.0 | Beginner

Developing applications on the iPhone or any new mobile device require additional consideration to be made because the application may be interrupted by many different ways as someone might make a phone call to the user which will force the user to switch away from the application. Alternative situations that could happen including switching off the application directly or having the device go into sleep mode, each of these events will have to be managed and reacted to accordingly if you want your application to be properly usable. Luckily, ActionScript 3.0 makes it very easy to track these events and reach to them by using a number of NativeApplication Events.

In order to be able to follow this tutorial you will have to be familiar with the basic concepts of AS3 Event Handling.

Why React to Status Changes?

Imagine that you are playing a game on your iPhone and someone calls you, the game will have to be interrupted in order for you to receive that phone call, if you do not arrange for your application to pause the action and show the pause menu you made the game will continue to run in the background even though the sound is muted. Of course, that will suck for the player because he would probably have lost the game by the time the call is finished. The same thing happens when the user presses on the sleep button, your game will not freeze on its own as the iPhone OS cannot determine the internal mechanisms by which your application is paused. In certain situations you will also want to make sure that you force the game to save when the user presses the home button. For all these reasons you have to react to status changes on the iPhone to make your game usable.

How to React to Status Changes?

Reacting to status changes on the iPhone is pretty easy as all you have to do is register for the corresponding event using the NativeApplication class. This class can be used directly in any iPhone or AIR application to react to application level events. The events have to register an event listener by the nativeApplicationa property of the class which corresponds to the actual application this way:

NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExit);
function onExit(e:Event):void {
//Code to be executed upon exit.
}

Using the code above the function onExit would be triggered when the user presses the Home button on his iPhone. This way the developer can execute a special code when the user presses that button so that he saves the data if he wishes to do so.

What Status Changes Are Available?

The statue changes events that you need to worry about on the iPhone are as follows:

  • Event.EXITING : This event is triggered when the user presses the Home button the device to exit the application.
  • Event.DEACTIVATE: This event is triggered when the application is deactivate to receive a phone call or when the user presses the Sleep button to put the device on sleep mode.
  • Event.ACTIVATE: This event is triggered when the application is reactivated after a phone call is finished or the user switches on the device from sleep mode.

Using all these events requires the same identical as the example shown above:

NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExit);
function onExit(e:Event):void {
//Code to be executed upon exit.
}
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onDeactivate);
function onDeactivate(e:Event):void {
//Code to be executed to pause the game.
}
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onActivate);
function onActivate(e:Event):void {
//Code to be executed to resume the game.
}

The actual code that you execute at each of these events will obviously depend on the nature of your application and could be as simple or complex as the system requires.

I hope that you learnt something new from this tutorial. If you have any questions or comments feel free to post them at the Republic of Code Forum.

- End of Tutorial