Domanda

Ho intenzione di implementare il multi-task nella mia app. Posso vedere molti metodi qui per farlo nel AppDelegate come applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, ...

Ma ... non vedo come dovrebbero essere usati, né perché non siano nei ViewControllers ... né per cosa sono qui.

Voglio dire: quando l'app entra in background, non so su quale vista si trovi il mio utente. E indietro, quando l'app viene in primo piano, come faccio a sapere cosa fare e come posso chiamare, ad esempio per aggiornare la visualizzazione?

Avrei capito se quei metodi fossero presenti in ogni controller di visualizzazione, ma qui, non vedo per cosa possono essere usati in modo concreto ...

Potete aiutarmi a capire come implementare le cose in questi metodi?

È stato utile?

Soluzione

Ogni oggetto riceve una notifica UIApplicationDidEnterBackgroundNotification quando l'app va in background.Quindi, per eseguire del codice quando l'app va in background, devi solo ascoltare la notifica dove vuoi:

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

Non dimenticare di rilasciare l'ascoltatore quando non hai più bisogno di ascoltarlo:

[[NSNotificationCenter defaultCenter] removeObserver:self];

E, soprattutto, puoi giocare allo stesso modo con le seguenti notifiche:

  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidBecomeActiveNotification

Altri suggerimenti

Non si trovano in nessuno dei tuoi controller di visualizzazione perché iOS adotta un modello di progettazione "delegato", in cui puoi essere certo che un metodo si attiverà su una classe (in questo caso, il delegato dell'app per la tua applicazione) quando richiesto.

Come processo di apprendimento, perché non inserisci i NSLog in questi metodi per vedere quando vengono licenziati?

- (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");
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top