-2

我有一个应用程序,它在后台每秒检查一次 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];
}
4

1 回答 1

0

检查目标是否在功能下启用了背景模式,并确保设置了位置更新模式。

于 2016-02-08T16:04:12.243 回答