If you want to track location changes you can do that with
Instantiate the manager in our controller's init
method:
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[locationManager requestWhenInUseAuthorization];
}
In the viewWillAppear:
method, start monitoring signification location changes.
if ([CLLocationManager locationServicesEnabled]) {
// Find the current location
[self.locationManager startMonitoringSignificantLocationChanges];
//rest of code...
}
Significant change monitoring is good because:
- It has lower power consumption than more frequent update types.
- "Significant" changes are variable but way less than the few miles used to trigger the push.
- The delegate will be called even if the app is in the background, allow using to continuously update the user's location, even when not using the app.
Here are the two important callbacks to implement, locationManager:didUpdateLocations:
and locationManager:didFailWithError:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// locations contains an array of recent locations, but this app only cares about the most recent
// which is also "manager.location"
}
Also add in your info.plist
<key>NSLocationUsageDescription</key>
<string>The application require location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>The application require location services to work</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>The application require location services to work</string>