1

I've updated to Xcode 6 (from Xcode 5) and now my app is not working anymore (I was quite proud it worked under IOS7). I have this "famous" debug output:

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

Of course I've been googling this message to find a solution but it seems that nothing's working. So I am asking for your advice.

Here is my header file:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapPoint.h"

#define kGOOGLE_API_KEY @"my google api"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

@interface XYZViewController : UIViewController <MKMapViewDelegate,       CLLocationManagerDelegate>

{
CLLocationManager *locationManager;
CLLocationCoordinate2D currentCentre;
int currenDist;
BOOL firstLaunch;
}

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

And here is my implementation file:

#import "XYZViewController.h"
#import "DetailViewController.h"

@interface XYZViewController ()

@end

@implementation XYZViewController

-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//Make this controller the delegate for the map view.
self.mapView.delegate = self;

// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];

//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];

//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];

//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

firstLaunch=YES;
}
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
mcpj
  • 95
  • 10

1 Answers1

0

You need to do as the error description tell you, and add a call to requestWhenInUseAuthorization or requestAlwaysAuthorization. This has been covered before, ie. iOS alertview for location permission doesn't pop up

The required changes could look like this:

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  //Make this controller the delegate for the map view.
  self.mapView.delegate = self;


  //Instantiate a location object.
  locationManager = [[CLLocationManager alloc] init];

  //Make this controller the delegate for the location manager.
  [locationManager setDelegate:self];

  //Set some parameters for the location object.
  [locationManager setDistanceFilter:kCLDistanceFilterNone];
  [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

  // Request use on iOS 8
  if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
    // Ensure that you can view your own location in the map view.
    [self.mapView setShowsUserLocation:YES];
  } else {
    [locationManager requestWhenInUseAuthorization];
  }

  firstLaunch=YES;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
    // Ensure that you can view your own location in the map view.
    [self.mapView setShowsUserLocation:YES];
  }
}

The above code do a version check (in the apple recommended way), you could also do a feature check, which is cleaner, but sometime cause problems in my own experience.

The tricky part is you need to provide a description of your use in the app Info.plist. Else the UIAlert will not appear and you will not be given permission. Go to the project/target settings in xcode, click the "Info" tab. Click the + button. For the key enter NSLocationWhenInUseUsageDescription and the value should be a string describing how you will use the user's location. You can also open the 'Info.plist' file under 'Supporting Files'.

I suspect this is why it hasn't worked for you yet.

Community
  • 1
  • 1
cypres
  • 366
  • 2
  • 8
  • I did add the key with its string value in my previous tries but did not work. Anyway your answer on such short notice helped me a lot and now my project is working! Thank you very much. Best Regards, – mcpj Oct 04 '14 at 22:10