不会弹出用于位置权限的iOS警报视图

问题描述:

我刚开始我的iOS 8项目,我也遇到了一个问题,我无法提出问题以获得许可。我将以下内容添加到我的info.plist

I just started my project for iOS 8 and i ran in too the problem that i cant get the question to pop up for permission. I added the following to my info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>

这是我的代码:

@interface ViewController () <MKMapViewDelegate>
@property (nonatomic, strong) UIPopoverController* userDataPopover;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic, retain) CLLocationManager *locationManager;
@end

@implementation ViewController

-(CLLocationManager *)locationManager
{
    if(!_locationManager) _locationManager = [[CLLocationManager alloc]init];
    return _locationManager;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate = self;
        [self.locationManager requestWhenInUseAuthorization];
        //[self.locationManager requestAlwaysAuthorization];

    [self.locationManager startUpdatingLocation];

    self.mapView.showsUserLocation = YES;
    [self.mapView showsUserLocation];
    [self.mapView setMapType:MKMapTypeStandard];
    [self.mapView setZoomEnabled:YES];
    [self.mapView setScrollEnabled:YES];
}

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


确保您的视图控制器实现CLLocationManagerDelegate和import #import CoreLocation / CoreLocation.h

Make sure your view controller implements CLLocationManagerDelegate and imports #import CoreLocation/CoreLocation.h

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setLocationManager:[[CLLocationManager alloc] init]];

    if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [self.mapview setShowsUserLocation:NO];
        [self.locationManager setDelegate:self];
        [self.locationManager requestWhenInUseAuthorization];
    }
    else{
        //Before iOS 8
        [self.mapview setShowsUserLocation:YES];
    }
}

添加此方法以侦听AuthorizationStatus更改

Add this method to listen for AuthorizationStatus changes

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
   if (status == kCLAuthorizationStatusDenied) {
       // permission denied
       [self.mapview setShowsUserLocation:NO];
   }
   else if (status == kCLAuthorizationStatusAuthorized) {
       // permission granted
       [self.mapview setShowsUserLocation:YES];
   }
}

最后确保您的plist具有以下条目作为指向出自Sanne

Finally make sure your plist has the following entries as pointed out by Sanne

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>