7

my issue is not get current location latitude and longitude in ios 8. i tried to set key in .plist file for ios 8 but but not call this method- didUpdateToLocation: please help this on issue.

my code is:-

- (void)viewDidLoad

{

[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

if(IS_OS_8_OR_LATER) {
    //[self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
}

}

Kalpit Gajera
  • 2,475
  • 1
  • 14
  • 24
Dhananjay Patel
  • 71
  • 1
  • 1
  • 2
  • Did iOS ask you for location permission? And if you test in the Simulator - do you have a test location set? – fluidsonic Oct 01 '14 at 05:29
  • FYI - do not check the iOS version. Check for the existence of the desired method. – rmaddy Oct 01 '14 at 05:32
  • yes i test iin simulator and set this two key in .plist file NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription – Dhananjay Patel Oct 01 '14 at 05:36
  • see this link http://rshankar.com/get-your-current-address-in-swift/ although its in swift but u can check for permissions etc. – Kundan Oct 01 '14 at 06:25

1 Answers1

35
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
-(void)viewDidLoad
{  
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
    NSUInteger code = [CLLocationManager authorizationStatus];
    if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
        // choose one request according to your business.
        if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
            [self.locationManager requestAlwaysAuthorization];
        } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
            [self.locationManager  requestWhenInUseAuthorization];
        } else {
            NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
        }
    }
}
[self.locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
    
    if (currentLocation != nil) {
        longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}

In iOS 8 you need to do two extra things to get location working: Add a key to your Info.plist and request authorization from the location manager asking it to start. There are two Info.plist keys for the new location authorization. One or both of these keys is required. If neither of the keys are there, you can call startUpdatingLocation but the location manager won’t actually start. It won’t send a failure message to the delegate either (since it never started, it can’t fail). It will also fail if you add one or both of the keys but forget to explicitly request authorization. So the first thing you need to do is to add one or both of the following keys to your Info.plist file:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

Both of these keys take a string

which is a description of why you need location services. You can enter a string like “Location is required to find out where you are” which, as in iOS 7, can be localized in the InfoPlist.strings file.

enter image description here

Community
  • 1
  • 1
Adarsh G J
  • 2,684
  • 1
  • 24
  • 25
  • 1
    Do not use `IS_OS_8_OR_LATER`. There are proper ways to check if an API is available or not. – rmaddy Apr 28 '16 at 17:37
  • @TheCodingArt There is no need to check the version at all. The proper way is to check the API using things like `respondsToSelector:`. – rmaddy May 04 '16 at 16:39
  • @rmaddy... that's actually a pretty bad way of approaching that. That's by far not a proper way (especially in today's age with Swift). respondsToSelector has common assumptions ignoring private & public implementations along other things. You should most definitely check the version rather rely on that. – TheCodingArt May 04 '16 at 16:41
  • Regardless of the approach, I was pointing out that if you need to check the version, adding your own custom macro is not only unreliable (as the systemVersion doesn't always reflect the proper iOS sub version), but is also redundant. – TheCodingArt May 04 '16 at 16:42