Matt Facer
Matt Facer

Reputation: 3105

iphone SDK - close app and/or go to sleep

I am using the following code to call some save methods when my app is either closed or the iphone goes to sleep. I'm just checking they are both OK and correct to use this way?

- (void)applicationWillResignActive:(UIApplication *)application {
    [self saveState];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self saveState];
}

The saveState function simply sets a few NSuserdefaults...

Thanks for any info!

Upvotes: 0

Views: 881

Answers (2)

Rob Napier
Rob Napier

Reputation: 299275

This is very normal, provided -saveState is reasonably fast (as in your example). In principle, the call in -applicationWillResignActive: isn't necessary, but it's a good idea nonetheless.

Upvotes: 1

Ben Gottlieb
Ben Gottlieb

Reputation: 85522

This seems reasonable. You probably don't need the call in -applicationWillResignActive:, as if the user, say, gets a call, -applicationWillResignActive: will be called, but if they TAKE the call, then -applicationWillTerminate: will be called, so you're covered.

Upvotes: 2

Related Questions