I'm trying to update users location even when app is terminated. I added maps, and background mode --> Location update to my .plist, and I set an local notification that will fire when location is updated. But it never got fired. I have this in AppDelegat.h:
@interface AppDelegate : UIResponder <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
and in AppDelegate.m
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:@"Location update!"];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
I tested this code on simulator with Freeway drive as location and it doesn't work.
EDIT
If I put the code under applicationDidEnterBackground
authorization request will close the second it's opened, because I'm using didFinishLaunchingWithOptions
. I know it's tracking location 24h because there's GPS symbol even when app is terminated.
EDIT 2
After testing it on my phone the code works. It wakes terminated app and sends local notification. It doesn't work on simulator but works in real life(device) :)