检查应用程序是否从非活动状态恢复到首次启动?

发布于 2024-11-05 20:39:36 字数 875 浏览 0 评论 0原文

我有一个 NSTimer,当我的应用程序退出活动状态时,我会停止它,稍后当应用程序再次变为活动状态时,我会重新启动它。我没有意识到的是,当我的应用程序第一次启动时, applicationWillResignActive 会触发(确实非常明显)。所以发生的事情是我的 NSTimer 未正确启动(当应用程序首次启动时)。我的问题是,有没有办法检查应用程序是否从非活动状态恢复到第一次启动?

- (void)applicationWillResignActive:(UIApplication *)application {
    // STOP CORE TIMER
    [[[self mapController] coreTimer] invalidate];
    [[self mapController] setCoreTimer:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // START CORE TIMER
    [[self mapController] setCoreTimer:[NSTimer 
        scheduledTimerWithTimeInterval:ONE_SECOND 
                                target:[self mapController]   
                              selector:@selector(timerDisplay:) 
                              userInfo:nil 
                               repeats:YES]];
}

I have a NSTimer that I am stopping when my application resigns active, which I later restart when the application again becomes active. What I did not realise was that applicationWillResignActive would fire when my application first started (pretty obvious really). So what is happening is my NSTimer is incorrectly starting (when the application first starts). My question is, is there a way to check for the application resuming from inactive as apposed to it first starting?

- (void)applicationWillResignActive:(UIApplication *)application {
    // STOP CORE TIMER
    [[[self mapController] coreTimer] invalidate];
    [[self mapController] setCoreTimer:nil];
}

.

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // START CORE TIMER
    [[self mapController] setCoreTimer:[NSTimer 
        scheduledTimerWithTimeInterval:ONE_SECOND 
                                target:[self mapController]   
                              selector:@selector(timerDisplay:) 
                              userInfo:nil 
                               repeats:YES]];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2024-11-12 20:39:36

applicationWillEnterForeground 似乎只在应用程序从背景。刚刚测试过,启动时不会被调用。

讨论

在iOS 4.0及更高版本中,该方法是
作为过渡的一部分调用
活动状态的背景。
您可以使用此方法撤消许多
您对您所做的更改
进入后申请
背景。对该方法的调用是
随后总是调用
applicationDidBecomeActive:方法,
然后将应用程序从
从非活动状态到活动状态。

There is the applicationWillEnterForeground which seems to only fire when the app comes back from the background. Just tested it, won't be called on launch.

Discussion

In iOS 4.0 and later, this method is
called as part of the transition from
the background to the active state.
You can use this method to undo many
of the changes you made to your
application upon entering the
background. The call to this method is
invariably followed by a call to the
applicationDidBecomeActive: method,
which then moves the application from
the inactive to the active state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文