简体   繁体   中英

ios 8 MapKit location will not start

Since ios 8 I cannot get the mapkit to start in the debugger. I'm using XCode Version 6.1 (6A1052d) on Mac OS X 10.9.5. And yes I've read and googled several suggestions but I can't get it to work.

I have added these lines in my myproject-info.plist;

</array>
<key>NSLocationUsageDescription</key>
<string>I need location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I need location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>I need location</string>
</dict>

This is how I try to initialice LocationManager in my first view viewDidLoad method;

CLLocationManager *locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];

Later, in another controller i programatically add a mapkit view like this;

mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:mapView];

[mapView setDelegate:self];
mapView.showsUserLocation = YES;

But I always get this in the output console;

2014-11-22 19:14:36.777 vind4r[1019:16660] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

Any ideas someone?

Kind regards, Jan Gifvars Stockholm

Try to move

mapView.showsUserLocation = YES;

To the -locationManager:didChangeAuthorizationStatus:

There you can check status for >= kCLAuthorizationStatusAuthorized and set showsUserLocation there:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status >= kCLAuthorizationStatusAuthorized) {
        self.mapView.showsUserLocation = YES;
    }
}

Declare

CLLocationManager *locationManager

in your .h file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM