背景位置服务不适用于IOS 7我最近升级了我的iOS设备以使用iOS 7。我们正在开发的一个应用程序使用后台定位服务来跟踪设备位置,我们的所有测试人员都报告说,该应用程序似乎不再跟踪iOS 7下的背景信息。我们已经证实,在设备上的设置中启用了该应用程序的回退,而之前的构建在iOS 6下运行得非常完美。即使该设备被循环使用,该应用程序在位置更新后也会重新启动。在iOS 7下,是否还需要做一些其他的工作呢?
3 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
我每1分钟重新启动一次位置管理器。 DIDUpdateLocations我允许位置管理器从设备中获取10秒的位置,然后关闭它(以节省电池)。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 30.0)
continue;
//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"];
[dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"];
[dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"];
//Add the vallid location with good accuracy into an array
//Every 1 minute, I will select the best location based on accuracy and send to server
[self.shareModel.myLocationArray addObject:dict];
}}//If the timer still valid, return it (Will not run the code below)if (self.shareModel.timer)
return;self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];[self.shareModel.bgTask beginNewBackgroundTask];//Restart the locationMaanger after 1 minuteself.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
selector:@selector(restartLocationUpdates)
userInfo:nil
repeats:NO];//Will only stop the locationManager after 10 seconds, so that we can get some accurate locations//The location manager will only operate for 10 seconds to save batteryNSTimer * delay10Seconds;delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];
}
一只斗牛犬
TA贡献1784条经验 获得超2个赞
开始位置更新;(测试精度为10米和100米,每次3次) 关闭设备屏幕,将应用程序放到后台; 将设备静止不动(例如在桌子上)30分钟。
- (void)tryRestartLocationManager{
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
int seconds = round(floor(now - locationManagerStartTimestamp));
if ( seconds > (60 * 8) ) {
[locationManager stopUpdatingLocation];
[locationManager startUpdatingLocation];
locationManagerStartTimestamp = now;
}}- 3 回答
- 0 关注
- 482 浏览
添加回答
举报
0/150
提交
取消
