背景位置服務不適用于IOS 7我最近升級了我的iOS設備以使用iOS 7。我們正在開發的一個應用程序使用后臺定位服務來跟蹤設備位置,我們的所有測試人員都報告說,該應用程序似乎不再跟蹤iOS 7下的背景信息。我們已經證實,在設備上的設置中啟用了該應用程序的回退,而之前的構建在iOS 6下運行得非常完美。即使該設備被循環使用,該應用程序在位置更新后也會重新啟動。在iOS 7下,是否還需要做一些其他的工作呢?
3 回答
汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
方法和解釋:-
我每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];
?}我收到了一些請求,要求在一定時間間隔內向服務器發送位置時添加示例代碼。我添加了示例代碼,并結合了背景任務管理器的修復來解決后臺運行較長時間的故障。
慕神8447489
TA貢獻1780條經驗 獲得超1個贊
開始位置更新;(測試精度為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 關注
- 499 瀏覽
添加回答
舉報
0/150
提交
取消
