0
votes

I'm working with CLLocationManagerDelegate in my static library when i run on iOS 7 device everything works perfectly but when i test it on another device with iOS 8.1.3 the delegate methods aren't getting called. i made a strong property @property (nonatomic, strong) CLLocationManager locationManager i've also added the appropriate key and string value (NSLocationAlwaysUsageDescription, requestAlwaysAuthorization) in the info.plist and this is my instance

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
self.locationManager.distanceFilter = kCLDistanceFilterNone; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];

if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
}
[self.locationManager startUpdatingLocation];

please help me to find out why delegate methods are not getting called !! why I'm not getting location updates??

5

5 Answers

2
votes

For iOS 8 you need to define the "Privacy - Location Usage Description" in the Info.plist.

Eg. Privacy - Location Usage Description = "Use your location to show near by stores".

This key specifies the reason for accessing the user’s location information.

2
votes

i hope its helps you. Call [locationManager requestWhenInUseAuthorization] inside if().....

if ([CLLocationManager locationServicesEnabled])
{
    locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];
}
else{

    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be showing past informations. To enable, Settings->Location->location services->on" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Continue",nil];
    [servicesDisabledAlert show];
    [servicesDisabledAlert setDelegate:self];
}


- (void)requestWhenInUseAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
    NSString *title;
    title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
    NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

    UIAlertView *alertViews = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Settings", nil];
    [alertViews show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    [locationManager requestWhenInUseAuthorization];
}
}


if ([alertView.message isEqualToString:@"To use background location you must turn on 'Always' in the Location Services Settings"])
{
    if (buttonIndex == 1)
    {
        // Send the user to the Settings for this app
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }
}

You MUST add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in your Plist file with some message "Your location is needed for this app"

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

CLLocation *location;
location =  [manager location];
CLLocationCoordinate2D coordinate = [location coordinate];;

globalObjects.longitude = [NSString stringWithFormat:@"%f",coordinate.longitude];
globalObjects.latitude = [NSString stringWithFormat:@"%f",coordinate.latitude];
}
1
votes
locationManager = [[CLLocationManager alloc] init];

[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:5];
[locationManager setHeadingFilter:5];
[locationManager setDelegate:self];

if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [locationManager requestAlwaysAuthorization];
}

[locationManager startUpdatingLocation];
0
votes

Can you see something like the following message in your console output?

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

I ran into this issue when i ported my apps to iOS8 the first time. There is a new restriction for having the following keys defined.

    /* Localized versions of Info.plist keys */

"NSLocationWhenInUseUsageDescription" = "for some reason, your app will use your location whenever the app is in foreground";

"NSLocationAlwaysUsageDescription" = "for some reason, your app will use your location whenever the app is in background";

These keys have to be defined in a localizable strings file called InfoPlist.strings in your project root directory. I placed this file on the wrong position once, which took me a long time to figure out, where the problem was.

The next thing is, that you should check, that the life time of your CLLocationManager, that you have requested the authorization for, has not exceeded when the user's acknowledgement should be processed. E.g. define a global CLLocationManager in your AppDelegate.

I hope that helps, kind regards, Peter

0
votes
if (![CLLocationManager locationServicesEnabled])
    {
        [[UtilityClass sharedObject]showAlertWithTitle:@"Location Services disabled" andMessage:@"App requires location services to find your current city weather.Please enable location services in Settings."];
    }
    else{
        [self stopLocationUpdating];
        if (locationManager==nil) {
            locationManager = [[CLLocationManager alloc] init];
            locationManager.delegate = self;
            locationManager.desiredAccuracy = kCLLocationAccuracyBest;

            #ifdef __IPHONE_8_0
            if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8"))
            {
                // Use one or the other, not both. Depending on what you put in info.plist
                //[self.locationManager requestWhenInUseAuthorization];
                [locationManager requestAlwaysAuthorization];
            }
            #endif
        }
        [locationManager startUpdatingLocation];



        [locationManager startUpdatingLocation];
    }

and add below code into your .plist file

NSLocationAlwaysUsageDescription=Application would like to use your location