I am working on location update.
When app is in foreground then app update the location using KVO,
self.mapView_locUpd.myLocationEnabled = YES;
[self.mapView_locUpd addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"myLocation"]) {
NSLog(@"Kvo....location");
CLLocation *location = [object myLocation];
self.currentLatitude= [NSString stringWithFormat:@"%f",location.coordinate.latitude];
self.currentLongitude=[NSString stringWithFormat:@"%f",location.coordinate.longitude];
[self updateCurrentLocation];
}
}
When app is in Background (Not killed) then app update the location using CLLocationManager,
-(void)initLocationmManager
{
if(self.locationManagerCurrent == nil)
self.locationManagerCurrent = [[CLLocationManager alloc]init];
self.locationManagerCurrent.delegate=self;
self.locationManagerCurrent.desiredAccuracy= kCLLocationAccuracyBestForNavigation; //provide precise location but more battery consumer
self.locationManagerCurrent.activityType = CLActivityTypeOtherNavigation;
self.locationManagerCurrent.pausesLocationUpdatesAutomatically = YES;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[self.locationManagerCurrent requestAlwaysAuthorization];
}
if ([self.locationManagerCurrent respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.locationManagerCurrent setAllowsBackgroundLocationUpdates:YES];
}
}
-(void)configureLocationManagerOnAppinBackground{
if(self.locationManagerCurrent == nil)
{
[self initLocationmManager];
}
[self.locationManagerCurrent startMonitoringSignificantLocationChanges];
if ( [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
{
[self.locationManagerCurrent startUpdatingLocation];
}
}
#pragma mark -CllocationmanagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
NSLog(@"Loctaionmanager....DidUpadetLoc");
CLLocation *location =[locations objectAtIndex:0];
if (location.horizontalAccuracy < 0) {
return;
}
self.currentLatitude= [NSString stringWithFormat:@"%f",location.coordinate.latitude];
self.currentLongitude=[NSString stringWithFormat:@"%f",location.coordinate.longitude];
[self updateCurrentLocation];
}
Using CllocationManager the location updates very well when app in background. Suppose in any case, the iOS suspend the app then iOS will launch my app in background and slowly start location updates.
Now if user killed the app from iPhone app switcher then there is not location update arise. So i want to update the location (continously or slowly) when app get killed by user. I tried so many thing to launch the app on killed. Pls help.