检查应用程序是否从非活动状态恢复到首次启动?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有
applicationWillEnterForeground
似乎只在应用程序从背景。刚刚测试过,启动时不会被调用。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.