UIApplicationDidBecomeActive vs UIApplicationWillEnterForeground difference

*爱你&永不变心* 提交于 2019-12-07 19:09:18

问题


I need to fire a method when the application become active again. I've found this useful question about the topic, but unfortunately it's not enough for me, I can't decide which one should I use in my case..

I have this method in the viewDidAppear:, and I would like to call it again everytime when the app become active again.

- (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];        
 }];
}

Based on the other question I placed these notifications into the viewDidLoad

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

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

And here is the methods that being called when the app become active again.

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

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

So could somebody tell me that in which one should I place the requestFullHistoryForChannel: method and why? As I've seen it in the console, the applicationEnteredForeground: has been called first, but I'm not sure that the sequence is always the same.


回答1:


applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.(inside appdelegate file)

call your method inside this method so it will get called when your application get restarted



来源:https://stackoverflow.com/questions/26680287/uiapplicationdidbecomeactive-vs-uiapplicationwillenterforeground-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!