iOS开发定位权限

定位权限

如果项目需要开启定位功能,需要在info.plist中设置Privacy - Location Always and When In Use Usage DescriptionPrivacy - Location When In Use Usage Description

info中配置.png

在需要开启定位权限的地方去弹起授权弹窗
需要设置CLLocationManager属性 (这里是个大坑,必须是属性,如果用局部变量弹不起来弹窗

@property (nonatomic, strong) CLLocationManager *locationManager;

设置代理及代理方法(可选,非必须设置,开启持续定位才会触发回调,弹窗授权后不触发)

<CLLocationManagerDelegate>
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"更新位置的回调");
}
 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"错误信息回调");
}

调起定位弹窗

- (void)GetLocationPermissionVerifcationWithController{
    BOOL enable = [CLLocationManager locationServicesEnabled];
    NSInteger state = [CLLocationManager authorizationStatus];
    
    if (!enable || 2 > state) {// 尚未授权位置权限
        if (8 <= [[UIDevice currentDevice].systemVersion floatValue]) {
            NSLog(@"系统位置权限授权弹窗");
            // 系统位置权限授权弹窗
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            [self.locationManager requestAlwaysAuthorization];
            [self.locationManager requestWhenInUseAuthorization];
        }
    }
}

已授权判断

BOOL enable = [CLLocationManager locationServicesEnabled];
CLAuthorizationStatus state = [CLLocationManager authorizationStatus];
if (enable && (state == kCLAuthorizationStatusAuthorizedAlways || state == kCLAuthorizationStatusAuthorizedWhenInUse)) {// 已授权位置权限
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • APP开发避免不开系统权限的问题,今天做定位时需要在不允许定位的时候做一些操作,所以,今天就大概的了解了一些。 权...
    SunshineBrother阅读 16,573评论 4 62
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 3,327评论 1 2
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 4,182评论 4 19
  • 背景 我们在App使用过程中包含获取用户位置的需求,于是在工程的Info.plis中,我们包含“Location ...
    wangplus7阅读 10,994评论 0 10
  • 在iOS开发中,定位是很多App都需要使用的功能。本文主要对iOS中的定位知识点进行介绍。本文代码环境为:Xcod...
    YungFan阅读 7,575评论 2 11