所以,我需要在app委托方法applicationDidEnterBackground中启动一个计时器:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
globalBackgroundTimer = [NSTimer timerWithTimeInterval:30 invocation:nil repeats:NO];
}
在app delegate.h中声明了这样的计时器:
extern NSTimer * globalBackgroundTimer;
当计时器运行时,我在视图控制器中收到后台位置更新(在plist中启用),并且我想在locationManager:didUpdateLocations中不断检查: 当计时器到期时,我可以结束位置更新。
... //code omitted
//this is called repeatedly when the app is in the background, and checks whether the global variable is instantiated, and if it has expired.
if(globalBackgroundTimer)
{
NSLog(@"timer alive");
if(!globalBackgroundTimer.isValid)
{
[locationManager stopUpdatingLocation];
NSLog(@"background timer invalid, stopping location updates");
}
}
但是我无法使它工作(Mach-O-linker错误), 但我也读到这种方法是不明智的。那么你们有什么建议呢?
答案 0 :(得分:0)
找出应用程序进入后台时存储计时器状态的机制(NSUserDefaults或Documents沙箱)。然后,当应用程序进入前台时,使用您保存的NSDate信息重新计算差异。
(我知道在你发布这个问题之前,你可能已经在做了什么。)