P. 1
iPhone App Programming Guide

iPhone App Programming Guide

|Views: 282|Likes:
Published by Lukasz81

More info:

Published by: Lukasz81 on Aug 22, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

08/22/2011

pdf

text

original

The foreground application always has precedence over other applications for the use of system resources
and hardware. Therefore, applications need to adjust their behavior to handle transitions between the
foreground and background. Specifically, applications moving to the background should follow these
guidelines:

DonotmakeanyOpenGLEScallsfromyourcode. You must not create anEAGLContext object or
issueanyOpenGLESdrawingcommandsofanykindwhilerunninginthebackground.Usingthesecalls
causes your application to be terminated immediately.

CancelanyBonjour-relatedservicesbeforebeingsuspended. When your application moves to the
background, and before it is suspended, it should unregister from Bonjour and close listening sockets
associated with any network services. A suspended application cannot respond to incoming service
requests anyway. Closing out those services prevents them from appearing to be available when they
actually are not. If you do not close out Bonjour services yourself, the system closes out those services
automatically when your application is suspended.

Bepreparedtohandleconnectionfailuresinyournetwork-basedsockets.Thesystemmayteardown
socket connections while your application is suspended for any number of reasons. As long as your
socket-based code is prepared for other types of network failures, such as a lost signal or network
transition,thisshouldnotleadtoanyunusualproblems.Whenyourapplicationresumes,ifitencounters
a failure upon using a socket, simply reestablish the connection.

Multitasking

37

2010-11-15 | © 2010 Apple Inc. All Rights Reserved.

CHAPTER2

The Core Application Design

Saveyourapplicationstatebeforemovingtothebackground. During low-memory conditions,
background applications may be purged from memory to free up space. Suspended applications are
purged first, and no notice is given to the application before it is purged. As a result, before moving to
the background, an application should always save enough state information to reconstitute itself later
if necessary.

Releaseanyunneededmemorywhenmovingtothebackground.Ifyourapplicationmaintainsalarge
in-memorycacheofobjects(especiallyimages),youshouldconsiderreleasingthosecacheswhenmoving
to the background. During low-memory conditions, the system considers the memory footprint of
applicationswhendecidingwhichonestopurge,withlargerapplicationsbeingthefirsttogo.Releasing
yourcacheshelpsalleviatememorypressureandmakesitlesslikelyapplicationswillneedtobepurged
later.

Stopusingsharedsystemresourcesbeforebeingsuspended. Applications that interact with shared
system resources such as the Address Book or calendar databases should stop using those resources
before being suspended. Priority for such resources always goes to the foreground application. When
your application is suspended, if it is found to be using a shared resource, it is terminated.

Avoidupdatingyourwindowsandviews. While in the background, your application’s windows and
viewsarenotvisible,soyoushouldnottrytoupdatethem.Althoughcreatingandmanipulatingwindow
and view objects in the background does not cause your application to be terminated, this work should
be postponed until your application moves to the foreground.

Respondtoconnectanddisconnectnotificationsforexternalaccessories. For applications that
communicate with external accessories, the system automatically sends a disconnection notification
when the application moves to the background. The application must register for this notification and
use it to close out the current accessory session. When the application moves back to the foreground,
a matching connection notification is sent, giving the application a chance to reconnect. For more
information on handling accessory connection and disconnection notifications, see External Accessory
Programming Topics.

Cleanupresourcesforactivealertswhenmovingtothebackground. In order to preserve context
when switching between applications, the system does not automatically dismiss action sheets
(UIActionSheet) or alert views (UIAlertView) when your application moves to the background. It is
up to you to provide the appropriate cleanup behaviorpriorto moving to the background. Forexample,
you might want to cancel the action sheet or alert view programmatically or save enough contextual
information to restore the view later (in cases where your application is terminated).

ForapplicationslinkedagainstaversionofiOSearlierthan4.0,actionsheetsandalertsarestilldismissed
at quit time so that your application’s cancellation handler has a chance to run.

Removesensitiveinformationfromviewsbeforemovingtothebackground. When an application
transitions to the background, the system takes a snapshot of the application’s main window, which it
then presents briefly when transitioning your application back to the foreground. Before returning from
yourapplicationDidEnterBackground: method, you should hide or obscure passwords and other
sensitive personal information that might be captured as part of the snapshot.

Dominimalworkwhilerunninginthebackground.Theexecutiontimegiventobackgroundapplications
is more constrained than the amount of time given to the foreground application. If your application
plays background audio or monitors location changes, you should focus on that task only and defer any
nonessential tasks until later. Applications that spend too much time executing in the background can
be throttled back further by the system or terminated.

38

Multitasking

2010-11-15 | © 2010 Apple Inc. All Rights Reserved.

CHAPTER2

The Core Application Design

If you are implementing a background audio application, or any other type of application that is allowed to
run in the background, your application responds to incoming messages in the usual way. In other words,
the system may notify your application of low-memory warnings when they occur. And in situations where
the system needs to terminate applications to free up even more memory, the application calls its delegate’s
applicationWillTerminate: method to perform any final tasks before exiting.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->