iOS update location even when app is terminated
Asked Answered
P

1

2

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) :)

Pennebaker answered 1/3, 2015 at 0:58 Comment(3)
How long did you wait for in freeway drive? According to Apple: "Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner." developer.apple.com/library/ios/documentation/CoreLocation/…Dialyze
From start to the end of the ride, and I didn't get any notification.Pennebaker
wait why is you code under didFinishLaunchingWithOptions? Should it not be under applicationDidEnterBackground?Hutchinson
D
1

It looks like you're using CLLocationManagerDelegate's deprecated function:

-(void)locationManager:(CLLocationManager *)manager 
        didUpdateToLocation:(CLLocation *)newLocation 
        fromLocation:(CLLocation *)oldLocation { 
}

Instead you should implement

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray *)locations {
}
Dialyze answered 1/3, 2015 at 1:33 Comment(2)
Above code will work only for foreground and background not in suspended condition .Italicize
Actually @iThink as the OP called [locationManager startMonitoringSignificantLocationChanges] I would expect this delegate to also get called in the background when a significant location change does occur.Dialyze

© 2022 - 2024 — McMap. All rights reserved.