在后台跟踪iOS设备动态更新超过10分钟

时间:2016-02-07 16:29:27

标签: ios cllocationmanager background-task devicemotion

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

1 个答案:

答案 0 :(得分:0)

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