简体   繁体   中英

didUpdateLocations delegate method not call in iOS 5.1 and earlier

I am using CoreLocation Framework for getting latitude and longitude in my project.

I have implement this delegate method in my .m file

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

   NSLog(@"Locations : %@", locations); 
}

But in iOS 5.0 & 5.1, this method is not call. and in iOS 6.0 or later it's call properly. so how to call this method in iOS 5.0 or later.

didUpdateLocations: did not exist in ios 5 or earlier. If you want to support those ios version then just use the deprecated didUpdateToLocation:fromLocation . Even though it is deprecated it still works in ios 6 and 7. That's the simplest solution.

You could have delegate methods for both, but that gets more complicated to manage. If you don't need to build track logs in the background then you don't really need didUpdateLocations: it was created to save battery power while collecting multiple points when running in the background.

See this answer...This method for iOS 5 (deprecated on iOS 6)and old version.

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

This method for iOS 6 and later version..

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

Refer:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html

As you can see locationManager:didUpdateLocations: is available only in iOS 6.0 and later.

You can always get the recent updated location from

CLLocationManger's location 

property.

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

the above delegate method is deprecated in iOS 6.

Instead of this,use:

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

This code (and the pausesLocationUpdatesAutomatically) would compile only in XCode 4.5 (where the base SDK is 6).

If you want to target iOS versions prior to 6.0 then use this macro

#define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

And wherever you are creating the CLLocationManager object

if(IOS_VERSION_GREATER_THAN_OR_EQUAL_TO (@"6.0"))
{
    locationManagerObj.pausesLocationUpdatesAutomatically = NO;
}

More details here:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM