2011-12-02 46 views
2

我明白,這個問題似乎已經已經在這裏找到答案: How do I get a background location update every n minutes in my iOS application?如何在iOS應用程序中每n分鐘獲得一次後臺位置更新?

然而,儘管該解決方案是在暗示,我會非常感激,如果有人可以張貼一些示例代碼,以這實際上是如何工作的。

非常感謝,

傑克

+0

@ wjans - 你可能在這裏用你最終使用的代碼來幫忙嗎?非常感謝! –

+0

你解決了這個問題嗎? – Luka

+0

你完成了這個任務嗎?請分享信息!我會非常感謝 – Cyril

回答

0

您需要在應用程序啓動訂閱核心位置。

一些這樣的事

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers; 

,並添加代表

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
+0

對不起,我應該說。我有這個排序的主要部分。我設置了一個按鈕,當按下按鈕時,獲取當前位置並將發送請求傳遞給服務器。我想要的是當應用程序處於後臺時,這會自動每30分鐘發生一次。 –

+0

即使應用程序處於後臺,核心位置代理也會被調用。 – nanjunda

+0

感謝您的幫助。這可行,但問題是我希望更新相距甚遠(例如每小時一次),這會消耗大量電池壽命。有沒有一種方法可以解決位置管理器的問題,然後再以較長的間隔再次停止。 –

0

試試這一個,你會得到lattitude和經度updated..in didUpdateToLocation ...

//in view did load 

locationManager = [[CLLocationManager alloc]init]; 
    if([CLLocationManager locationServicesEnabled]) 
    { 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 
     locationManager.distanceFilter = 1000.0f; 
     [locationManager startUpdatingLocation]; 
     } 


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
appDelegateObj.latitude= [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.latitude]; 
    appDelegateObj.longitude = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.longitude]; 
    } 
+0

只需發佈與上述相同的評論。感謝您的幫助: –

+0

對不起,我應該說。我有這個排序的主要部分。我設置了一個按鈕,當按下按鈕時,獲取當前位置並將發送請求傳遞給服務器。我想要的是當應用程序處於後臺時,這會自動每30分鐘發生一次。 –

+0

所以你可以添加按鈕和按鈕按下這個調用線程30分鐘。因此每30分鐘的線程將會調用它自己的動作並在線程動作中添加此方法,以便每隔30分鐘撥打一次。 – Kartik

相關問題