Constantly tracking user location on iOS

2019-03-20 14:05发布

问题:

I need to constantly monitor the position of an user to notify him when he gets near something interesting. Which is the correct way to achieve this?

I have not been able to keep a timer running in the background (to periodically update user's position on the server), I also read that subscribing to significant location changes could have make me receive location updates even when the app is not running, but i can't get it happen. Am I on the right path? Are there any other options?

EDIT - There is a requisite that i should have exposed and that is probably making me loose the focus on the real problem:

  • Interesting "stuff" around the user has not a fixed location, so before sending a push I have to be sure that the user is currently in that location (of course i can assume it for n minutes).

回答1:

You can set up monitoring location stuff in you VC as below

in viewDidLoad method do as below

CLLocationManager locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;(Accuracy according to your need)
[locationManager startUpdatingLocation];

than you have to overrite below two optional delegate methods of CLLocationManagerDelegate protocol

for iOS6+

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}

and for iOS 2 to 6

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

in these methods you will get updated location. use it as you want. every time location updated these method get calls.



回答2:

What you need is geofencing. In iOS it's called "region monitoring". You can start with startMonitoringForRegion: in the CLLocationManager guide. More detail is in the Region Monitoring Guide

In iOS, regions associated with your app are tracked at all times, including when your app is not running. If a region boundary is crossed while an app is not running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it is woken up and given a short amount of time (around 10 seconds) to handle the event.



回答3:

As far as I know, significant change location update is the only way to update current location when the app is running in background or not suspended(terminated) by system. when that happens, be sure to check UIApplicationLaunchOptionsLocationKey and initialise CLLocationManager to handle location change notification. If the app is running in foreground, that's simple case.

By the way, in order to let system keep updating location to your app, you must enable maps feature and background modes(location updates) for the project.

read Apple location/Map documents for more details.



回答4:

Disclaimer: I work for Cintric

We had a similar problem at Cintric and actually made a tool just for this purpose. It will track the location in the background even after the app has been quit, using only 1% battery too!

You can download the .framework file, drag it into your project and initialize with one line of code: [CintricFind initWithApiKey:@"YOUR_API_KEY_HERE" andSecret:@"YOUR_SECRET_HERE"];

You can get an API key for free.

There are docs here: https://www.cintric.com/docs/ios

And there is a blog post explaining further here: http://www.blog.cintric.com/?p=121