iphone 4 sdk : detect return from background mode
Asked Answered
G

2

26

How can I detect that an app has just returned from "background mode"? I mean, I don't want my app to fetch data (each 60 sec) when the user press the "home button". But, I'd like to make some "special" update the first time the app is in foreground mode.

How can I detect these two events:

  1. app going to background mode
  2. app going to foreground mode

Thanks in advance.

François

Gustafsson answered 4/10, 2010 at 17:10 Comment(0)
P
48

Here's how to listen for such events:

// Register for notification when the app shuts down
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationWillTerminateNotification object:nil];

// On iOS 4.0+ only, listen for background notification
if(&UIApplicationDidEnterBackgroundNotification != nil)
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

// On iOS 4.0+ only, listen for foreground notification
if(&UIApplicationWillEnterForegroundNotification != nil)
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationWillEnterForegroundNotification object:nil];
}

Note: The if(&SomeSymbol) checks ensure that your code will work on iOS 4.0+ and also on iOS 3.x - if you build against an iOS 4.x or 5.x SDK and set the deployment target to iOS 3.x your app can still run on 3.x devices but the address of relevant symbols will be nil, and therefore it won't try to ask for notifications that don't exist on 3.x devices (which would crash the app).

Update: In this case, the if(&Symbol) checks are now redundant (unless you really need to support iOS 3 for some reason). However, it's useful to know this technique for checking if an API exists before using it. I prefer this technique than testing the OS version because you are checking if the specific API is present rather than using outside knowledge of what APIs are present in what OS versions.

Pasteurism answered 4/10, 2010 at 17:20 Comment(0)
O
5

If you implement a UIApplicationDelegate, you can also hook into functions as part of the delegate:

- (void)applicationDidEnterBackground:(UIApplication *)application {
   /*
   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. 
 If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
   */
    NSLog(@"Application moving to background");
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
  /*
   Called as part of the transition from the background to the active state: here you can undo many of the changes made on entering the background.
   */
    NSLog(@"Application going active");
}

For the protocol reference see http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Overnight answered 27/12, 2011 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.