一、首先。在Info.plist里面添加
Key:Required background modes
Value:App registers for location updates
- (void)scheduledTimer
{
UIApplication* app = [UIApplication sharedApplication];
self.backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"applicationD in Background");
}];
//加入定时器,用来控制后台运行时间
NSTimer *runTimer = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:@selector(startLocation)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:runTimer forMode:NSRunLoopCommonModes];
}
- (void)startLocation
{
NSLog(@"每隔10秒来一次!!!!!");
}