iOS 开发IT梦之队iOS高质量博客

iOS位置服务权限相关

2016-04-12  本文已影响6311人  JerryLMJ

整个iOS系统的定位服务是否开启

#import <CoreLocation/CoreLocation.h>

[CLLocationManager locationServicesEnabled];

当前应用定位服务授权

@interface RootViewController () <CLLocationManagerDelegate>

@property (nonatomic,strong) CLLocationManager * manager;

@end
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
[_manager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    NSString *errorString;
    NSLog(@"定位失败原因: %@",[error localizedDescription]);
    switch([error code]) {
        case kCLErrorLocationUnknown:
            // do something...
            break;
        case kCLErrorDenied:
            // do something...
            break;
        
         ......

    }
}

定位的错误信息

typedef NS_ENUM(NSInteger, CLError) {
    // 目前位置是未知的,但CL将继续努力
    kCLErrorLocationUnknown  = 0,         // location is currently unknown, but CL will keep trying
    // 获取用户位置或范围被拒绝
    kCLErrorDenied,                       // Access to location or ranging has been denied by the user
    // 一般情况下,网络相关的错误
    kCLErrorNetwork,                      // general, network-related error
    // 标题不能确定
    kCLErrorHeadingFailure,               // heading could not be determined
    // 位置区域监测被用户拒绝
    kCLErrorRegionMonitoringDenied,       // Location region monitoring has been denied by the user
    // 注册区域不能监控
    kCLErrorRegionMonitoringFailure,      // A registered region cannot be monitored
    // CL不能立即初始化区域监控
    kCLErrorRegionMonitoringSetupDelayed, // CL could not immediately initialize region monitoring
    // 如果这个防护事件被提交,提交将不会出现
    kCLErrorRegionMonitoringResponseDelayed, // While events for this fence will be delivered, delivery will not occur immediately
    // 地理编码没有结果
    kCLErrorGeocodeFoundNoResult,         // A geocode request yielded no result
    // 地理编码产生一部分结果
    kCLErrorGeocodeFoundPartialResult,    // A geocode request yielded a partial result
    // 地理编码被取消
    kCLErrorGeocodeCanceled,              // A geocode request was cancelled
    // 延迟模式失败
    kCLErrorDeferredFailed,               // Deferred mode failed
    // 延迟模式失败了,因为位置更新禁用或暂停
    kCLErrorDeferredNotUpdatingLocation,  // Deferred mode failed because location updates disabled or paused
    // 延迟模式不支持当前精准度
    kCLErrorDeferredAccuracyTooLow,       // Deferred mode not supported for the requested accuracy
    // 延迟模式不支持距离过滤器
    kCLErrorDeferredDistanceFiltered,     // Deferred mode does not support distance filters
    // 延迟模式请求取消前一个请求
    kCLErrorDeferredCanceled,             // Deferred mode request canceled a previous request
    // 测距杆不能执行
    kCLErrorRangingUnavailable,           // Ranging cannot be performed
    // 测距失败
    kCLErrorRangingFailure,               // General ranging failure
};

1,在Info.plist添加配置 (可根据情况任选其一,也可以两者都添加)



2,向系统注册权限(可根据情况任选其一,也可以两者都添加,与Info.plist中添加的配置对应)

[_manager requestWhenInUseAuthorization];
[_manager requestAlwaysAuthorization];

3,当应用启动时,系统会根据应用的注册授权弹出提示框请求用户授权
(弹框的描述信息与Info.plist中配置的描述信息是一直的)


4,当我们点击允许的时候,定位服务开始生效。
此时持续调用代理函数

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"定位中....");
}

当点击不允许的使用,分别调用代理函数

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    NSLog(@"授权状态改变");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSString *errorString;
    NSLog(@"定位失败原因: %@",[error localizedDescription]);
}

5,我们再来看看手机设置中的界面


版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

上一篇下一篇

猜你喜欢

热点阅读