执行从睡眠中引入应用程序的功能

时间:2011-05-02 23:07:51

标签: iphone objective-c cocoa-touch ios

嘿伙计们,我确信这是非常直接的,但我无法在任何地方找到这些信息。我需要我的应用程序在从后台打开时重新加载一些信息(不是新的打开)。任何想法如何做到这一点?

谢谢

2 个答案:

答案 0 :(得分:5)

您的应用可以覆盖- (void)applicationWillEnterForeground:(UIApplication *)application中的UIApplicationDelegate

您还可以让您的控制器成为UIApplicationWillEnterForegroundNotification的观察者。根据Apple Docs:在应用程序离开后台状态之前不久发布,即成为活动应用程序。

NIB文件中带控制器的示例代码,否则覆盖- (id)init

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wakeUp:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)wakeUp:(NSNotification *)pNotification {
    NSLog(@"Name: %@", [pNotification name]);
    NSLog(@"Object: %@", [pNotification object]);
    NSLog(@"I am waking up");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

答案 1 :(得分:4)

查看applicationDidBecomeActive协议中applicationWillEnterForegroundUIApplicationDelegate的文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html