背景位置服務不適用于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 關注
- 475 瀏覽
添加回答
舉報
0/150
提交
取消
