0

I am having much trouble trying with requesting the location services authorization. I know there are other posts on this forum, but I did not solve my problem with their solution.

This is the error popping up in xCode:

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

I have added both the required keys for the Plist.

Another important point is that when I start it in the simulator, I can go into the setting manually and enable location services and then the App does work. However, when I restart the App it does not work and I get the same message above.

I want to prompt the user with the option to enable location services. Unfortunately, this code does not prompt the authorization for location services.

Please help I have been pulling my hair out for hours. 1

 - (void)viewDidLoad {
[super viewDidLoad];

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

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



//Initialize the map and specifiy bounds
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];

//specifcy resizing
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

//show the User's location and set tracking mode
self.myMapView.showsUserLocation = YES;
self.myMapView.userTrackingMode = MKUserTrackingModeFollow;

//add the VIEW!!
[self.view addSubview:self.myMapView];



 }

And here is the Function I want to call

 - (void)requestAlwaysAuthorization
 {
     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 *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Settings", nil];
    [alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    [self.locationManager requestAlwaysAuthorization];
    }
}

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
  if (buttonIndex == 1) {
     // Send the user to the Settings for this app
     NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
     [[UIApplication sharedApplication] openURL:settingsURL];
 }
 }
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
tennis779
  • 338
  • 2
  • 6
  • 19

1 Answers1

0
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

This is the delegate method about location service You can get your AuthorizationStatus from status.Such as kCLAuthorizationStatusDenied

You simply call

[self.locationManager requestAlwaysAuthorization];in viewdid load,and monitor AuthorizationStatus in delegate method above,if user deny your location service,show a alertview

Leo
  • 24,596
  • 11
  • 71
  • 92
  • When I implement this method and then call didChangeAuthorizaitionStatus, it does not enter this delegate method. Does it go to this method after viewDidLoad has completed or does it go to the method directly after the status has changed. Clearly though the location servicies are not set even after this requestAlwaysAuthroization is called – tennis779 Nov 03 '14 at 03:41
  • Everytime after viewdidload,this delegate method will be called.You simply call [self.locationManager requestAlwaysAuthorization] in view did laod.Do not use if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]);And first time this app installed,a alert will show to inform user that this app requires loaction service – Leo Nov 03 '14 at 04:06
  • Do not call delegate method,it will be auto called when the app is opened.You just need to write your code into delegate method to act with different codition – Leo Nov 03 '14 at 04:09