0
votes

I am trying to get the users location in iOS 8 but I get the following error code: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

I have called those methods in my code and I have also created the necessary entries in my Info.plist file. What could be the problem?

Here is my code:

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate,CLLocationManagerDelegate>

@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@property (nonatomic,strong) CLLocationManager *locationManager;

@end

And my ViewController.m

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>



@interface ViewController ()

@end

@implementation ViewController
@synthesize mapView;
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)


- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate = self;

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if(IS_OS_8_OR_LATER) {
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
    [self.locationManager startUpdatingLocation];

    self.mapView.showsUserLocation = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

What could be wrong?

2
have you added NSLocationAlwaysUsageDescription key to you property list ?Onik IV
You should either request "when in use" or "always" authorization - not both. Also I would suggest you use respondsToSelector to determine if the authorisation methods are available rather than examining the iOS version. Have you set a breakpoint to confirm the authorisation request methods are being invoked?Paulw11

2 Answers

2
votes

now you need to add more items in your info.plist file:

NSLocationWhenInUseUsageDescription = "your text" (it may be " ")

NSLocationAlwaysUsageDescription = "your text" (it may be " ")

2
votes

Add this to your plist

<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

And this lines of code

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)


    if(IS_OS_8_OR_LATER) {
            [locationManager requestAlwaysAuthorization];
        }

This worked for me. Hope will work for you too :)