Ketan Shinde Ketan Shinde - 1 month ago 12
iOS Question

Region monitoring and background

I have little doubt and wants to know is it necessary to reassign all regions again to location manager, if it receive the post notification for

appEnterInBackGround
?

here is some code snippet.

- (IBAction)startAction:(id)sender
{
for (Geofencing *gObjects in plotingArrays) {
CLCircularRegion *getRegion = [self dictToRegion:gObjects];
[monitorLocationManager startMonitoringForRegion:getRegion];
}
}


So when app enters in back ground i did like this:

# pragma mark - BackGround Notification
-(void)applicationEnterBackground
{
monitorLocationManager = [selectRouteController sharedLocationMonitor];
monitorLocationManager.delegate = self;
for (Geofencing *gObjects in plotingArrays) {
CLCircularRegion *getRegion = [self dictToRegion:gObjects];
[monitorLocationManager startMonitoringForRegion:getRegion];
}
}


So is it necessary to reassign the regions again to location manger when app enter in background? OR it will automatically monitor once the region is assigned to location manager on
startAction:
action

UPDATE1:

+ (CLLocationManager *)sharedLocationMonitor {
static CLLocationManager *locationMonitor;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

locationMonitor = [[CLLocationManager alloc] init];
locationMonitor.desiredAccuracy =
kCLLocationAccuracyBestForNavigation;

locationMonitor.activityType =
CLActivityTypeAutomotiveNavigation;
[locationMonitor requestAlwaysAuthorization];

if(IS_OS_9_OR_LATER){
locationMonitor.allowsBackgroundLocationUpdates = YES;
}

if(SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"8.4")){
locationMonitor.pausesLocationUpdatesAutomatically = NO;
}
});
return locationMonitor;
}


PLIST:

App plist configuration

Answer

No, You don't need to restart monitoring for region when your app enters in background. It will automatically monitor region if you have configured it.

You need to configure following in info.plist:

<key>NSLocationAlwaysUsageDescription</key>
<string>I want to get your location Information in background</string>

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

And you also need to set AllowsBackgroundLocationUpdates to yes.

 [monitorLocationManager setAllowsBackgroundLocationUpdates:YES];