我開始使用IOS開發,而且我對這門語言非常陌生。IOS-位置更新時間間隔
我試圖開發一個應用程序,將追蹤背景上的設備的位置。我已經遵循了一些教程並提供了一個記錄位置更新的代碼。
-(void)CurrentLocationIdentifier
{
locationManager = [CLLocationManager new];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self CurrentLocationIdentifier];
}
它的工作原理。由於我已經配置了.plist文件,因此它被註冊爲在後臺運行,它無止境地記錄了位置。
但我需要更新定期,定義的時間間隔,否則它會是一個電池殺手。我意識到有關背景中的位置更新有很多問題,但我的嘗試沒有成功,我被卡住了。
我已經試過回採更新和位置更新偵聽器內安排與的NSTimer開始:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);
[locationManager stopUpdatingLocation];
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:@selector(CurrentLocationIdentifier)
userInfo:nil
repeats:NO];
}
哪裏CurrentLocationIdentifier
是再次請求啓動更新的方法。
但它第一次運行,然後不開啓預定的任務!我究竟做錯了什麼?應該以其他方式完成嗎?
但是,這不會像監視每一個小小的變化一樣消耗盡可能多的電量嗎? – LcSalazar 2014-09-26 22:04:18
不,重要的位置更新改變使用WiFi和蜂窩塔監視以及GPS來推遲位置更新,直到用戶移動了相當長的距離(通常至少幾百米)。我建議你觀看今年和前幾年的位置服務WWDC視頻和/或閱讀Apple提供的位置服務節目指南。他們討論有效使用位置的最佳實踐。 – Paulw11 2014-09-26 22:06:32
隨着你的計時器方法,你試圖「第二次猜測」的iOS。這種方法可以讓iOS協調整個設備的位置更新,如果另一個應用程序使用詳細的位置,您的應用程序將自動獲得更頻繁的更新。如果沒有,那麼iOS將管理位置更新以提高電池效率 – Paulw11 2014-09-26 22:08:29