You have two options here:
1) Regular location tracking.
This type of tracking works with kCLAuthorizationStatusAuthorizedWhenInUse and kCLAuthorizationStatusAuthorizedAlways . When the CLLocationManager started location tracking once, it will receive location updates in the locationManager delegate method locationManager:didUpdateLocations: application can go into a paused state, but when the location manager receives a new location, the application goes into the background and processes the new location in the delegate method. How to set up location manager:
- (void)viewDidLoad { [super viewDidLoad]; self.locationManager = [[CLLocationManager alloc] init]; // Setup location tracker accuracy self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Distance filter self.locationManager.distanceFilter = 50.f; // Assign location tracker delegate self.locationManager.delegate = self; // This setup pauses location manager if location wasn't changed [self.locationManager setPausesLocationUpdatesAutomatically:YES]; // For iOS9 we have to call this method if we want to receive location updates in background mode if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ [self.locationManager setAllowsBackgroundLocationUpdates:YES]; } [self.locationManager startUpdatingLocation]; }
2) Tracking location changes.
This type of tracking only works with kCLAuthorizationStatusAuthorizedAlways authorization. It receives a new location only every 500 meters, so the distance filter and the required accuracy do not work here. An application can go into a suspended state or even be interrupted by the system, but when updating a location, the application goes into the background state and gets the location in the delegate method locationManager:didUpdateLocations:
If the application was a UIApplicationLaunchOptionsLocationKey system, it will be restarted in the background with the UIApplicationLaunchOptionsLocationKey key in the launch options in the didFinishLaunchingWithOptions application didFinishLaunchingWithOptions . How to configure this type for tracking:
- (void)viewDidLoad { [super viewDidLoad]; self.locationManager = [[CLLocationManager alloc] init]; // Assign location tracker delegate self.locationManager.delegate = self; // For iOS9 we have to call this method if we want to receive location updates in background mode if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ [self.locationManager setAllowsBackgroundLocationUpdates:YES]; } [self.locationManager startMonitoringSignificantLocationChanges]; }
You should note that both of these methods do not guarantee that your application will not go into a suspended state.
In addition, if the application was interrupted by the user (for example, using the application switcher by scrolling), tracking in the background will not work.
UPDATE (corresponding to comments)
Here are my sample code that work for me:
For regular tracking . Run the example, provide access to the user's location, click the "Start" button to start updating the location. To check the location in the simulator, select "Debug"> "Location"> "Freeway" in the simulator menu. Now you can move the application to the background using the "Home" button (Ctrl + Shift + H). Leave the simulator for more than 10 minutes, and all this time the application will receive locations. When you return to the application, you will see red pins on the map.
For significant changes . Launch the application and test as in the previous example.
Monitoring Major changes can only be started using the [self.locationManager startMonitoringSignificantLocationChanges]; method [self.locationManager startMonitoringSignificantLocationChanges];
UPDATE (iOS 11)
Location Tracking Changes in iOS 11
iOS 11 also makes some significant changes to existing APIs. One of the affected areas is location tracking. If your application uses the location only while it is in the foreground , as most applications do, you may not have to change anything at all; however, if this is one of those applications that continuously track the location of users throughout the day, you should probably book this summer to make some changes in tracking and testing possible usage scenarios.
follow this link: https://mackuba.eu/2017/07/13/changes-to-location-tracking-in-ios-11/