What I want to do: iPhone is sleeping. In the background location manager is scanning for a beaconRegion. If this region is recognized my app should start beacon ranging to verify the distance to the beacon. The beacon ranging should stop when the region is left.
So here is my code:
-(void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region
{
NSLog(@"********** Beacon Region entered: %@", region);
self.locationManager.allowsBackgroundLocationUpdates = YES;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
[self doBeaconRanging:(CLBeaconRegion *)region];
}
-(void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray<CLBeacon *> *)beacons
inRegion:(CLBeaconRegion *)region
{
NSLog(@"locationManager didRangeBeacons inRegion.");
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
NSLog(@"locationManager didUpdateLocations");
}
-(void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region
{
NSLog(@"********** Beacon Region left **********");
[self.locationManager stopUpdatingLocation];
NSSet *regions = [self.locationManager monitoredRegions];
for(CLBeaconRegion *region in regions)
{
[self.locationManager stopRangingBeaconsInRegion:region];
}
}
This works fine when the region if entered while the app is in foreground. didEnterRegion is fired and the beacon ranging starts. Even if I send the app and the iPhone to sleep the beacon ranging goes on and on (just to test, in real app I will stop beacon ranging after approx 3 minutes).
But when didEnterRegion is fired while the iPhone is locked the behaviour is different. Location update and beacon ranging starts, but only for ca. 10 seconds. After 10 seconds the iPhone goes back to sleep and location updates and beacon ranging stops. When I wake up the app the location updates start immediately and the beacon ranging as well. But of course, I do not want to wake up the app. Everything should run in the background without the need to open the app.
If I start location updates when app is in foreground and never stop it, than everything works fine. Location updates coming in all the time even after hours in background. But I think this is no solution due to battery life.
How can I start updating Location when didEnteredRegion is fired while the app is in background?