iPhone Development 101

iPhone 101

Code Tips

Resources

More

iOS apps support multitasking. This means that when the user presses the "home" button on the phone, your app doesn't quit; instead it moves into the background. If you're running a looping timer, you must pause it; if your app needs to save data, you should do it in the applicationWillResignActive method (instead of the applicationWillTerminate method). This ensures that your data is saved if the OS kills your app while it's still in the background.

These methods are called when the app enters the background. (These methods should be implemented in your app delegate class.) You can also have other objects listen for the specific notifications.

- (void)applicationWillResignActive:(UIApplication *)application {
    // Called prior to entering the background. This is a good place to save your data.    
    // After calling this method, the application also posts a UIApplicationWillResignActiveNotification notification to give interested objects a chance to respond to the transition.

    NSLog(@"app will resign active");
    // [self saveData];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Called when the user presses the home button to "quit" the application. (The app is suspended.) Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    
    // The application also posts a UIApplicationDidEnterBackgroundNotification notification around the same time it calls this method to give interested objects a chance to respond to the transition.
    NSLog(@"app did enter background");
    // [self stopTimers];
}

Keep in mind that applicationWillResignActive is also called whenever the app is interrupted by the OS, such as when a call comes in, or a SMS or push notification pops up. See the iOS Programming Guide for details.

These methods are called when the app returns from the background (i.e, the user presses the app icon to launch the app again):

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as the app resumes. Here you can undo many of the changes made on entering the background - restart timers, resume networking, etc.

    // The application also posts a UIApplicationWillEnterForegroundNotification notification shortly before calling this method to give interested objects a chance to respond to the transition.
    
    NSLog(@"app will enter foreground");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // After calling this method, the application also posts a UIApplicationDidBecomeActiveNotification notification to give interested objects a chance to respond to the transition.    
    
    NSLog(@"app did become active");
}

Note that none of these methods actually enable your app to run code in the background. iOS4 supports a limited set of tasks your app can run in the background. View the Apple documentation for more details:

References

TopHome