Reputation: 401
I have an app that checks if the user approaches one of my client's stores and when he does, the app sends him a notification telling him he is near a store.
Of course I'm using geofencing (locationManager: didEnterRegion
) and I have more than 20 stores so on locationManager: didUpdateLocations
I'm sorting the 20 nearest stores to the user.
I'm configuring my CLLocationManager
this way on the AppDelegate
and then setting my ViewController
locationManager
property to the object (because I want to use the same locationManager
on the AppDelegate
as well:
-(void)configureLocationManager
{
//Initializing "locationManager"'s(CLLocationManager)
self.locationManager=[[CLLocationManager alloc] init];
//Setting "locationManager"'s(CLLocationManager)'s delegate to "monitorLocationVC"(monitorLocationViewController)
self.locationManager.delegate=self.monitorLocationVC;
//Setting "locationManager"'s(CLLocationManager)'s distance filter to 10
self.locationManager.distanceFilter=10;
//Setting "locationManager"'s(CLLocationManager)'s activityType to navigation
self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
//setting "locationManager"'s(CLLocationManager) desiredAccuracy to "best"
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//Setting "locationManager"'s(CLLocationManager)'s pausesLocationUpdatesAutomatically to NO
self.locationManager.pausesLocationUpdatesAutomatically=NO;
//If OS version is 9 or above - setting "allowsBackgroundLocationUpdates" to YES
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
}
Until now everything worked fine, but now I see that the app started not to notify for stores in the background after a few minutes it is running (notifies about 15 minutes and then suddenly stops).
Note: Sometimes when I launch my app from the home screen (just a normal launch) the app crashes, could it be the problem?
Thank you!
Upvotes: 1
Views: 399
Reputation: 487
check this documentation and add background fetch mode: documentation apple
and add this code:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
if ([self.locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
Upvotes: 0