Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of background/foreground methods in AppDelegate

I'm planning to implement multi-task in my app. I can see many methods here to do that in the AppDelegate like applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, ...

But.... I don't see the way they should be used, nor why they are not in the ViewControllers... Nor what they are here for.

I mean : when the app enter in background, i don't know on which view my user is. And back, when the app comes into foreground, how would I know what to do and what I may call, to update the view for example ?

I would have understood if those methods where in each view controller, but here, I don't see what they can be used for in a concrete way...

Can you help me to understand the way to implement things into those methods ?

like image 946
Oliver Avatar asked Jan 31 '11 01:01

Oliver


People also ask

What is a background and foreground?

In simple terms, the foreground is that part of the image that is closest to the camera. The background is the part of the image that is further away from the camera.

What is foreground and background in apps?

Foreground refers to the active apps which consume data and are currently running on the mobile. Background refers to the data used when the app is doing some activity in the background, which is not active right now. This is due to the fact that whether they are active or not, apps consume data.

What is AppDelegate and methods in Swift?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

What does foreground mean in iOS?

The foreground contains the applications the user is working on, and the background contains the applications that are behind the scenes, such as certain operating system functions, printing a document or accessing the network.


2 Answers

Each object receive a UIApplicationDidEnterBackgroundNotification notification when the app goes in background. So to run some code when the app goes in background, you just have to listen to that notification where you want :

[[NSNotificationCenter defaultCenter] addObserver:self                                          selector:@selector(appHasGoneInBackground:)                                              name:UIApplicationDidEnterBackgroundNotification                                            object:nil]; 

Don't forget to release the listener when you don't need to listen to it anymore :

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

And best of the best, you can play the same way with the following notifications :

  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidBecomeActiveNotification
like image 104
Oliver Avatar answered Sep 19 '22 20:09

Oliver


They are not in any of your view controllers because iOS adopts a 'delegate' design pattern, where you can be assured that a method will fire upon a class (in this case, the App Delegate for your application) when required.

As a learning process, why don't you just put NSLog's in those methods to see when they're fired?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {          // Override point for customization after application launch.     NSLog(@"didFinishLaunchingWithOptions");     [self.window makeKeyAndVisible];      return YES; }   - (void)applicationWillResignActive:(UIApplication *)application  {     /*      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.      Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.      */     NSLog(@"applicationWillResignActive"); }   - (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(@"applicationDidEnterBackground"); }   - (void)applicationWillEnterForeground:(UIApplication *)application  {     /*      Called as part of  transition from the background to the active state: here you can undo many of the changes made on entering the background.      */     NSLog(@"applicationWillEnterForeground"); }   - (void)applicationDidBecomeActive:(UIApplication *)application  {     /*      Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.      */     NSLog(@"applicationDidBecomeActive"); }   - (void)applicationWillTerminate:(UIApplication *)application  {     /*      Called when the application is about to terminate.      See also applicationDidEnterBackground:.      */     NSLog(@"applicationWillTerminate"); } 
like image 38
Alan Zeino Avatar answered Sep 19 '22 20:09

Alan Zeino