【问题标题】:iOS alertview for location permission doesn't pop up位置权限的 iOS alertview 不会弹出
【发布时间】:2014-09-18 10:34:05
【问题描述】:

我刚开始我的 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 中工作正常,现在有了权限更改,一切都被打破了。将密切关注这一点。
  • 我在我的代码中找到了解决方案。当我忽略这一行时:[self.locationManager startUpdatingLocation];一切都开始工作了!希望对你有帮助!
  • 为您的 plist 中的精彩文字点赞!

标签: ios ios8 xcode6


【解决方案1】:

确保您的视图控制器实现 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>

【讨论】:

    【解决方案2】:

    iOS 10 的新功能 现在在 plist 文件中添加位置权限

    如果不在 plist 中添加权限,则权限弹出窗口不显示

    在 plist 中添加以下权限

    1.隐私-使用时的位置使用说明

    2.Privacy - Location Always 使用说明

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2014-11-17
      相关资源
      最近更新 更多