0

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.

Sommm
  • 527
  • 4
  • 22
  • Have you tried to update the location on push notification? – Cy-4AH Apr 26 '17 at 13:56
  • yes. I tried to update the location on local as well as remote notification (silent notification). But once app killed by user and notification arrive, the user has to launch the application by notification click or by app icon click then only location start to update. – Sommm Apr 26 '17 at 14:24

1 Answers1

0

You should explore https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

You need UIBackgroundModes with location key.

Also you need to request Privacy - Location Always Usage and provide "Privacy - Location Always Usage Description" key in your Info.plist

This will allow iOS to partial relaunch your app when user kills it. iOS will return a key UIApplicationLaunchOptionsLocationKey to the app delegate method didFinishLaunchingWithOptions.

Also this answer should help you: How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended

Community
  • 1
  • 1
Mike M
  • 1
  • 1
  • Thanx Mike...!!! yes i handled this too and i also took the reference of your link. It was the same link i used to refer before. Any other way to debug that. – Sommm May 05 '17 at 10:20