Use of background/foreground methods in AppDelegat

2019-01-21 09:25发布

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 ?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-01-21 09:33

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");
}
查看更多
放荡不羁爱自由
3楼-- · 2019-01-21 09:34

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
查看更多
登录 后发表回答