本文介绍了UIApplicationDidBecomeActive 与 UIApplicationWillEnterForeground 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序再次激活时,我需要触发一个方法.我发现这个 有关该主题的>有用问题,但不幸的是,这对我来说还不够,我无法决定在我的情况下应该使用哪个..

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..

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

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

基于另一个问题,我将这些通知放入 viewDidLoad

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");
}

那么有人可以告诉我应该将 requestFullHistoryForChannel: 方法放在哪个位置,为什么?正如我在控制台中看到的那样, applicationEnteredForeground: 已被首先调用,但我不确定顺序是否始终相同.

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.

推荐答案

applicationDidBecomeActive:——让你的应用知道它即将成为前台应用.使用此方法进行最后一分钟的准备.(在 appdelegate 文件中)

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

这篇关于UIApplicationDidBecomeActive 与 UIApplicationWillEnterForeground 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:50