2016-02-19 38 views
6

我試圖讓應用程序始終跟蹤用戶的GPS,這個應用程序是一種汽車GPS跟蹤器,可以隨時獲取驅動程序的位置並將其發送到服務器。一直運行的iOS GPS跟蹤應用程序

我試圖將「位置更新」添加到「背景模式」,但應用程序會在進入背景10分鐘後自動掛起。

有沒有辦法讓這個應用程序一直運行並獲得GPS位置?

謝謝。

回答

7

您有兩種選擇:

1)定期跟蹤定位。
這種跟蹤類型適用於kCLAuthorizationStatusAuthorizedWhenInUsekCLAuthorizationStatusAuthorizedAlways授權。當CLLocationManager開始跟蹤位置後,它將通過委託方法locationManager:didUpdateLocations:接收位置更新。應用程序可以進入掛起狀態,但是當位置管理器接收到新的位置應用程序時,它將轉到後臺狀態並在委託方法中處理新位置。如何設置位置經理:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.locationManager = [[CLLocationManager alloc] init]; 

    // Setup location tracker accuracy 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 

    // Distance filter 
    self.locationManager.distanceFilter = 50.f; 

    // Assign location tracker delegate 
    self.locationManager.delegate = self; 

    // This setup pauses location manager if location wasn't changed 
    [self.locationManager setPausesLocationUpdatesAutomatically:YES]; 

    // For iOS9 we have to call this method if we want to receive location updates in background mode 
    if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ 
     [self.locationManager setAllowsBackgroundLocationUpdates:YES]; 
    } 

    [self.locationManager startUpdatingLocation]; 
} 


2)意義位置的改變跟蹤。
這種類型的跟蹤僅適用於kCLAuthorizationStatusAuthorizedAlways授權。它每500米只接收一個新的位置,所以距離過濾器和期望的精度在這裏不起作用。應用程序可以進入暫停狀態,甚至可以由系統終止,但是當位置更新應用程序進入後臺狀態並在代理方法locationManager:didUpdateLocations:中接收到位置時。
如果應用程序被系統終止,它將在後臺重新啓動,UIApplicationLaunchOptionsLocationKey鍵啓動選項didFinishLaunchingWithOptions應用程序委託方法。如何建立這種類型的跟蹤:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.locationManager = [[CLLocationManager alloc] init]; 

    // Assign location tracker delegate 
    self.locationManager.delegate = self; 

    // For iOS9 we have to call this method if we want to receive location updates in background mode 
    if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ 
     [self.locationManager setAllowsBackgroundLocationUpdates:YES]; 
    } 

    [self.locationManager startMonitoringSignificantLocationChanges]; 
} 


你應該注意到,這兩種方法並不能保證你的應用程序不會去暫停狀態。
另外,如果應用程序被用戶終止(例如通過刷卡從應用程序切換器),則後臺中的位置跟蹤將無法工作。


UPDATE(對應於評論)

這裏是我的代碼示例,爲我工作:
For Regular tracking。運行示例,提供對用戶位置的訪問,點擊開始按鈕以啓動位置更新。要在模擬器中測試位置,請在模擬器菜單調試>位置>高速公路驅動器中進行選擇。現在,您可以通過主頁按鈕將應用推送到背景(Command + Shift + H)。離開模擬器超過10分鐘,所有這段時間的應用程序將收到位置。當你回到應用程序時,你會在地圖上看到紅色的針腳。
For Significant changes。運行應用程序並按照與前一示例相同的方式進行測試。
監控的重大變化只能由法[self.locationManager startMonitoringSignificantLocationChanges];

UPDATE(iOS版11)

更改位置跟蹤iOS中11

iOS的11也使得對現有API的一些重大變化開始。其中一個受影響的區域是位置跟蹤。 如果您的應用僅在位於前臺的位置使用位置,與大多數應用一樣,您可能不必更改任何內容;然而,如果它是一天中持續跟蹤用戶位置的應用程序之一,那麼您應該在今年夏天預訂一些時間,對如何跟蹤和測試可能的使用場景進行一些更改。

請點擊此鏈接:https://mackuba.eu/2017/07/13/changes-to-location-tracking-in-ios-11/

+0

謝謝你的回答,對於選項1,你說,當接收到新的位置,狀態從暫停的背景下,我一直在測試幾天去和這種情況下從不與我發生了,應用程序回來只有在手動運行應用程序時再次生活,然後在10分鐘後暫停,爲什麼? – TMMDev

+0

對於選項2,有沒有辦法讓startMonitoringSignificantLocationChanges以某種方式運行[self.locationManager startUpdatingLocation]?這將使其至少每500米跑至少 – TMMDev

+1

更新回答您的意見。我沒有看到你的代碼,所以我提供了鏈接到我的代碼示例在這裏爲選項1和2. – shpasta

0

在回答溶液中的評論1(我不能評論任何地方還):你似乎並沒有解決問題,因爲您的應用程序被暫停,不更新10分鐘後再次出發。

我有同樣的問題:我已成立setAllowsBackgroundLocationUpdatesYES,和我有NSLocationAlwaysUsageDescription鑰匙在我Info.plist,但我的應用程序也可以用來停止10分鐘後跟蹤位置。

我解決它通過添加兩個NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescriptionInfo.plist文件,所以它看起來是這樣的:

<key>NSLocationAlwaysUsageDescription</key> 
    <string>This app needs to use your location</string> 

    <key>NSLocationWhenInUseUsageDescription</key> 
    <string>This app needs to use your location</string> 
0

集it.it取電池的功率,但在Background.OS您的應用程序運行不暫停您的應用

[self.locationManager setPausesLocationUpdatesAutomatically:NO];