【发布时间】:2014-05-21 00:51:29
【问题描述】:
我有 NSTimer,它根据我当前的时间调用 startUpdatingLocation 或 StopUpdatingLocation 的方法。如果我当前的时间少于晚上 8 点,它将停止更新位置,但我的计时器将在后台连续运行。我既没有删除我的应用程序,也没有在后台删除我的应用程序。
我需要在后台连续运行计时器,并且应该在当前时间是第二天早上 8 点时开始更新位置。现在我的应用程序正在后台运行,但我的位置不会在第二天早上 8 点开始更新。我在 iOS 7 中进行了测试。只有在我从后台启动应用程序后,它才会开始更新位置。
谁能建议我如何在 iOS 7 的后台连续运行计时器?
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIDevice *device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
backgroundSupported = YES;
}
if (backgroundSupported)
{
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
NSTimer *preventSleepTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0] interval:20.0f target:self selector:@selector(switchingOnLocation) userInfo:nil repeats:YES];
self.mTimer = preventSleepTimer;
[preventSleepTimer release];
// Add the timer to the current run loop.
[[NSRunLoop currentRunLoop] addTimer:self.mTimer forMode:NSRunLoopCommonModes];
}
}
【问题讨论】:
标签: ios7 nstimer core-location