0

When trying to display of current location on the map after adding the requestWhenInUseAuthorization into the plist file, still I'm getting error:

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

I tried to solve several ways. But still failed. I am confused which part I am missing. My full code is:

I added a mapView on the top of view controller and then mapped it with .h file.

.h file code:

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

@interface ViewController : UIViewController <MKMapViewDelegate>

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

@end

.m file code:

#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

@synthesize mapView = _mapView;

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

    [self.mapView.userLocation self];
    [self.mapView setShowsUserLocation:YES];
}


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
    [self.mapView setRegion:region animated:YES];
}


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

@end

Can any body please tell me which thing I am missing here. I'm new in iOS and I tried to solve couple of hours.

adarksun
  • 371
  • 1
  • 6
  • 20

2 Answers2

0

You need to call requestWhenInUseAuthorization or requestAlwaysAuthorization to prompt the user if he will allow location tracking. Then you need to handle the response and use location only if the user allows it.

It should look like this:

  1. Find the status of authorization
  2. If the status is CLAuthorizationStatus.NotDetermined, call requestWhenInUseAuthorization or requestAlwaysAuthorization and process the result
  3. If the authorization status is kCLAuthorizationStatusRestricted or kCLAuthorizationStatusDenied, your app is not permitted to use location services and you should abort your attempt to use them

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • do i need to use any delegation method to do that? – adarksun Mar 15 '15 at 18:30
  • I don't think so, you just call it before you tell the app that you want to use location. The only "gotcha" is that you need to wait for the response after you display the prompt. – MirekE Mar 15 '15 at 18:34
0

Try this,

Create an object for CLLocationManager.

Add the following code inside of your viewDidLoad.

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

Don't forgot to add

 CLLocationManagerDelegate
prakash
  • 73
  • 7
  • I added CLLocationManagerDelegate into the .h file (@interface ViewController : UIViewController ) and added that code block inside viewDidLoad method. Still, I am getting the error. – adarksun Mar 15 '15 at 18:48
  • Have you looked at this? [http://stackoverflow.com/a/24718342/1520704](http://stackoverflow.com/a/24718342/1520704) – prakash Mar 15 '15 at 18:58
  • Yes i saw that. I see there is two property but for mine I have only one property in .h file. I tried to create a simple app which will show the user current location. – adarksun Mar 15 '15 at 19:05