2014-10-31 61 views
2

我需要在应用程序再次激活时触发一个方法。我发现这个useful关于这个话题的问题,但不幸的是这对我来说还不够,我不能决定在我的情况下应该使用哪一个。UIApplicationDidBecomeActive vs UIApplicationWillEnterForeground difference

我在viewDidAppear:中有这种方法,我想每当应用再次激活时再次调用它。

- (void)viewDidLoad { 

[PubNub requestFullHistoryForChannel:x-channel withCompletionBlock:^(NSArray *msg, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) { 

     AppDelegate *delegateArray = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
     delegateArray.mainArray = [NSMutableArray arrayWithArray:msg]; 

     [self setupContent];   
}]; 
} 

基于其他的问题,我把这些通知到viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationIsActive:) 
              name:UIApplicationDidBecomeActiveNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationEnteredForeground:) 
              name:UIApplicationWillEnterForegroundNotification 
              object:nil]; 

这里是当应用程序变得活跃起来的是被调用的方法。

- (void)applicationIsActive:(NSNotification *)notification { 
    NSLog(@"Application Did Become Active"); 
} 

- (void)applicationEnteredForeground:(NSNotification *)notification { 
    NSLog(@"Application Entered Foreground"); 
} 

所以可能有人告诉我,其中一个我应该放置requestFullHistoryForChannel:方法,为什么?正如我在控制台中看到的那样,applicationEnteredForeground:已被首先调用,但我不确定序列总是相同的。

+1

尝试打开控制中心,通知中心,Siri的,多任务用户界面等,而你的应用程序是打开的,看看哪些通知发送。 – 2014-10-31 17:34:42

回答

0

applicationDidBecomeActive: - 让您的应用程序知道它即将成为前台应用程序。使用此方法对于任何最后一分钟的准备。(的appdelegate文件中)

调用这个方法里面您的方法,因此将被调用时,你的应用程序来重新启动