【问题标题】:Location and accelerometer updates in the background under iOS 10iOS 10 下后台位置和加速度计更新
【发布时间】:2016-10-30 21:48:00
【问题描述】:

我的应用会收集位置和加速度计信息,并在用户请求时继续在后台收集。加速度计信息以大约 100 Hz 的频率收集,并且以高速率收集它是至关重要的。该应用在旧版本的 iOS 下运行良好,但在 iOS 10 下失败。我正在寻找一种在后台继续收集加速度计信息的方法。

使用的方法与Receive accelerometer updates in background using CoreMotion framework中的方法类似。我启动位置管理器:

latitude = 0.0;
longitude = 0.0;
altitude = 0.0;
horizontalAccuracy = -1.0;
verticalAccuracy = -1.0;
if (locationManager == nil && [CLLocationManager locationServicesEnabled]) {
    self.locationManager = [[CLLocationManager alloc] init];

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
        [locationManager requestWhenInUseAuthorization];
    }

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.pausesLocationUpdatesAutomatically = NO;
    [locationManager startUpdatingLocation];
}

我实现了 locationManager:didUpdateLocations:

- (void) locationManager: (CLLocationManager *) manager didUpdateLocations: (NSArray *) locations {
    printf("locationManager:didUpdateLocations:\n");
    CLLocation *location = locations.lastObject;
    latitude = location.coordinate.latitude;
    longitude = location.coordinate.longitude;
    altitude = location.altitude;
    horizontalAccuracy = location.horizontalAccuracy;
    verticalAccuracy = location.verticalAccuracy;
}

我启动运动管理器:

motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 1.f/trialFrequency;
[[NSOperationQueue mainQueue] setMaxConcurrentOperationCount: 10];
[motionManager startAccelerometerUpdatesToQueue: [NSOperationQueue mainQueue]
                                    withHandler: ^(CMAccelerometerData *accelerometerData, NSError *error) {
                                        // Flag any error.
                                        if (error) {
                                            NSLog(@"error getting accelerometer update: %@", error);
                                        }
                                        <<<process data>>>
                                    }
 ];

已检查目标、功能、后台模式、位置更新。

在 iOS 9 及之前的版本下,运动信息以 100 Hz 左右的速率记录没有问题,但在 iOS 10 下,一旦应用进入后台,运动信息就会停止。

iOS 10下有什么办法让运动信息在后台继续?

【问题讨论】:

  • 我建议向 Apple 提交错误报告。 iOS 10 对传感器的后台使用做了一些奇怪的改变,这可能就是其中之一。无论如何,如果它以前有效,那么您就有了一个合理的用例。

标签: ios accelerometer ios10 locationmanager cmmotionmanager


【解决方案1】:

从 iOS 9 开始,需要一个新的调用来启用位置事件的后台处理。可用于开启和关闭后台处理,但默认为关闭,因此必须手动设置才能在 iOS 8 及之前的代码中恢复后台处理。

在设置位置管理器以允许后台处理时添加此行:

    locationManager.allowsBackgroundLocationUpdates = YES;

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2013-10-03
    相关资源
    最近更新 更多