我有一个应用程序,它在后台每秒检查一次 iPhone 的重力。当我在连接到 Xcode 时运行此代码时,程序在后台运行了 30 多分钟。但是,当它没有连接到 Xcode 时,它会在后台运行 10 分钟前停止运行。我使用位置更新和动作更新:
- (void)applicationDidEnterBackground:(UIApplication *)application {
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
NSLog(@"End Background Tasks");
}];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[locationManager requestAlwaysAuthorization];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
locationManager.allowsBackgroundLocationUpdates = YES;
}
[locationManager startUpdatingLocation];
[self.motionManager startDeviceMotionUpdates];
}