Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get location when app goes to background or phone is locked?

Tags:

ios

iphone

I'm trying using locationManager to get my location and update it to my webservice.

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
fromLocation:(CLLocation *)oldLocation {
    //some code here
}

It's normal, but if my app goes to the background or phone screen is locked, updating location may be paused. How do I get the location anyway?

like image 742
Let's Bi Avatar asked Nov 04 '22 14:11

Let's Bi


2 Answers

Read the full blog post on it: iPhone Background GPS: accurate to 500m, not enough for foot traffic

Here is a tutorial: iOS Multitasking: Background Location

like image 143
iOS Avatar answered Nov 15 '22 04:11

iOS


I recommend you set up a handler such as this example:

TTLocationHandler

Configure the handler for the recency and accuracy required. Set up a listener for the notification "LocationHandlerDidUpdateLocation". Put your code to upload the data in a method called on receipt of that notification.

If you do not require fine grain detail, I would advise you use the battery saving options of significantLocationChange monitoring in background. In that case you will not need to add background mode in info.plist and you will not waste resources unnecessarily. The app will be awakened and your method called only on significant movement of the user. There is configurable options in the handler to require constant monitoring in background and/or foreground, and to set recency and accuracy thresholds.

See the LMPinTracker class for example of how to respond and save your data to web or locally. See the thread Invoke get current coordinates every few seconds without NSTimer for discussion of use.

like image 40
Dean Davids Avatar answered Nov 15 '22 06:11

Dean Davids