2017-01-15 18 views
1

我試圖在暫停應用程序時接收位置更新 - 根據我已閱讀的文檔,這應該不是問題,而我我遵循所有必需的步驟,但是一旦應用程序被暫停,位置更新似乎不會發生。儘管已啓用後臺位置更新,CLLocationManager委託在應用程序暫停後仍未收到位置更新

我見過類似的帖子,其中包含使用startMonitoringSignificantLocationChanges的解決方案,但根據我對文檔的理解,我不需要在startUpdatingLocation之上實現它。

根據文檔,如果啓用了後臺位置更新,系統將被暫停,但當有新的位置數據時將被喚醒。

由於文檔狀態「在暫停應用程序的情況下,系統被喚醒的應用,提供更新的位置經理的委託」我希望我可以處理該位置更新相同的,如果應用程序是爲在前景或背景中。

我在Capabilities選項卡和info.plist中選擇了位置更新,關鍵的UIBackgroundModes包括值的數組中的位置。

我缺少什麼?

作爲參考,這是我如何聲明我的CLLocationManager。

lazy var locationManager: CLLocationManager = { 
    let locationManager = CLLocationManager() 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 
    locationManager.activityType = .fitness 
    locationManager.requestAlwaysAuthorization() 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.allowsBackgroundLocationUpdates = true 
    locationManager.pausesLocationUpdatesAutomatically = true 
    return locationManager 
}() 

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1

「你能在你的Xcode項目功能選項卡的背景模式部分位置的支持。(你也可以通過包括在位置值的UIBackgroundModes鍵啓用此支持你)啓用此模式並不會阻止系統暫停應用程序,但它確實告訴系統,只要有新的位置數據要傳送,它就應該喚醒應用程序。因此,該鍵有效地讓應用程序在後臺運行以處理位置更新。「

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

「該系統提供位置更新背景位置信息的應用時,應用程序在前臺運行,在後臺運行時,或懸浮在懸浮應用程序的情況下,系統被喚醒啓動應用程序,將更新傳遞給位置管理器的代理,然後儘快將應用程序恢復到暫停狀態。「

回答

1

我有同樣的問題,並防止beeing暫停該應用程序的唯一方法是設置

locationManager.pausesLocationUpdatesAutomatically = false 

pausesLocationUpdatesAutomatically設置爲true系統使應用程序處於暫停狀態,不會接收位置直到用戶手動將應用程序再次移至前臺。