Question

Je prévois de mettre en œuvre multi-tâches dans mon application. Je vois ici beaucoup de méthodes pour le faire dans le AppDelegate comme applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, ...

Mais .... Je ne vois pas la façon dont ils devraient être utilisés, ni pourquoi ils ne sont pas dans les ViewControllers ... ni ce qu'ils sont ici.

Je veux dire: quand l'application entrer en arrière-plan, je ne sais pas où mon utilisateur voir. Et en arrière, lorsque l'application est en premier plan, comment pourrais-je savoir ce qu'il faut faire et ce que je peux appeler, mettre à jour la vue par exemple?

J'aurais compris si ces méthodes où dans chaque contrôleur de vue, mais ici, je ne vois pas ce qu'ils peuvent être utilisés pour de façon concrète ...

Pouvez-vous me aider à comprendre la façon de mettre en œuvre les choses dans ces méthodes?

Était-ce utile?

La solution

Chaque objet reçoit une notification de UIApplicationDidEnterBackgroundNotification lorsque l'application va en arrière-plan. Donc, pour exécuter un code lorsque l'application va en arrière-plan, il vous suffit d'écouter cette notification où vous voulez:

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

Ne pas oublier de libérer l'auditeur lorsque vous n'avez pas besoin d'écouter plus:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Et le meilleur des meilleurs, vous pouvez jouer de la même manière avec les notifications suivantes:

  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidBecomeActiveNotification

Autres conseils

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");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top