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.