Search code examples
ioscore-locationcllocationmanagerdeployment-target

CLLocationManager didUpdateLocations or didUpdateToLocation


I my iOS i am using CLLocationManager, but i am not sure which delegate method to use to get the location updates. There are two methods

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

and

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations

I know that i should be using the second one because the first one is deprecated but then i have set the deployment target for my app to be 6.0. So which one i should use? In the image attached enter image description here, just beside this method

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

it says 6.0. So what does it mean its deprecated in iOS 6.0 or its available till 6.0. My guess is that, i should be using

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations

and it should be fine. Any suggestions?


Solution

  • I am also faced the same issue in my old project, So I tried the following method, then working fine for me

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

    The delegate above is deprecated in iOS 6. Now the following should be used:

    - (void)locationManager:(CLLocationManager *)manager 
         didUpdateLocations:(NSArray *)locations
    

    In order to get the last position, simply get the last object of the array:

    [locations lastObject]
    

    In other words, [locations lastObject] (new delegate) equals newLocation (old delegate)

    example

    iOS6 and above

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *newLocation = [locations lastObject];
    CLLocation *oldLocation;
    if (locations.count >= 2) {
        oldLocation = [locations objectAtIndex:locations.count-1];
    } else {
        oldLocation = nil;
    }
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
    
    }
    

    if you use in iOS6 and below add the following method also

     - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [self locationManager:locationManager didUpdateLocations:[[NSArray alloc] initWithObjects:newLocation, nil]];
    }
    

    for additional reference follow this tutorial