iOS APP 在背景取得使用者位置(Get location at background)

簡單整理一下iOS如何在背景持續取得使用者位置,本例在收到新定位後會發送一個local notification給自己 在專案中的設定 Target => Capabilities => Background Modes,Switch to ON 選取Location updates 在plist中加入描敘(這會在提醒使用者同意使用定位時出現) Privacy - Location Always and When In Use Usage Description Privacy - Location Always Usage Description Privacy - Location When In Use Usage Description 實作 1. 在AppDelegate中加入import import CoreLocation import UserNotifications 2. AppDelegate實作CLLocationManagerDelegate class AppDelegate: UIResponder , UIApplicationDelegate , CLLocationManagerDelegate { 3. 加入location manager相關實作 使用locationManager.startMonitoringSignificantLocationChanges(),而非locationManager.startUpdatingLocation。使用方法可參考 官方文件 ,要特別注意的是這個方法取得新定位的觸發時間點在這份 官方文件 裡有說明如下,大意大概是移動距離500公尺以上才會觸發,且觸發間隔大於5分鐘(我的解讀及實際測試是在行進中,大約每5分鐘觸發一次) Note Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently ...