IOS 後臺運行GPS (IOS iPhone Object-C)

當程序切換到後臺後仍然開始gps 的方法其實很簡單
只需要在 info.plist 里加上 "Required background modes" 這個項
然後添加這個項的1個 item "App registers for location updates"

然後你的app 如果在前臺開啓了 gps ,切到後臺後 依然會持續更新gps 信息
調用代理方法. 

在這裏要提醒下, 在後臺開啓gps會持快速續消耗電池
所以更好的方法是吧 在切換到後臺後, 把原來高精度的gps 調用換成低精度的,來增加電池壽命

在 applicationDidEnterBackground 中加入

if ([CLLocationManager significantLocationChangeMonitoringAvailable]) 

        {

            // Stop normal location updates and start significant location change updates for battery efficiency.

            [[GlobalVariables locationManager] stopUpdatingLocation];

            [[GlobalVariables locationManager] startMonitoringSignificantLocationChanges];

        }

        else 

        {

            NSLog(@"Significant location change monitoring is not available.");

        }


在applicationDidBecomeActive 中加入 

     if ([CLLocationManager significantLocationChangeMonitoringAvailable]) 

    {

// Stop significant location updates and start normal location updates again since the app is in the forefront.

[[GlobalVariables locationManager] stopMonitoringSignificantLocationChanges];

[[GlobalVariables locationManager] startUpdatingLocation];

}

else 

    {

NSLog(@"Significant location change monitoring is not available.");

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章