Reputation: 14408
I m new to the iOS. When i press home button in iPhone, application goes into suspend mode is what i know, in the program, how can i capture this event, and clear my local datas? Kindly advice.
Upvotes: 0
Views: 984
Reputation: 8808
While you can implement the UIApplicationDelegate methods discussed by others, it is often more convenient (and arguably cleaner) to have objects that need to do clean up register themselves for the corresponding notifications:
UIApplicationDidEnterBackgroundNotification
UIApplicationWillResignActiveNotification
E.g.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myCleanupMethod:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
(If you go this route, don't forget to remove the observer when the observing object is deallocated.)
Upvotes: 0
Reputation: 16124
Use app delegate applicationWillResignActive method and UIBackgroundTaskIdentifier if needed. For example:
- (void)applicationWillResignActive:(UIApplication *)application {
UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
[application endBackgroundTask:backgroundTaskIdentifier];
//your cleanup code
}];
}
Upvotes: 0
Reputation: 16714
You can use the following method from within your app delegate:
- (void)applicationWillResignActive:(UIApplication *)application
Apple explains when this delegate method is called:
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Upvotes: 2
Reputation: 3873
Inside your delegate, put the code you want to call inside of these. One fires every time you background the application and the other fires when you come back.
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
Upvotes: 1