iOS位置权限的警告框未弹出

7

我刚开始我的iOS 8项目,遇到了一个问题,就是无法弹出请求权限的问题。我已经在我的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];
}

整个位置服务在iOS8中已经破坏了我的应用程序。在iOS7中一切正常,现在由于权限更改,一切都出问题了。我会继续关注这个问题。 - Fogmeister
1
我在我的代码中找到了解决方案。当我省略了这一行:[self.locationManager startUpdatingLocation]; 时,一切开始正常工作!希望这对你有所帮助! - Sanne
2
你的plist文件里的文本真棒,已点赞! - Paul Cezanne
2个回答

7

请确保您的视图控制器实现了CLLocationManagerDelegate并导入#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更改

-(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所指出的:

<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>

1

这是 iOS 10 的新功能。

现在需要在 plist 文件中添加位置权限。

如果没有在 plist 中添加权限,则不会弹出权限请求提示。

请在 plist 中添加以下权限:

1. 隐私 - 使用期间定位权限说明

2. 隐私 - 始终定位权限说明


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接