iOS 监测蓝牙与定位是否开启并跳转设置中开启

(一)、获取定位权限

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

iOS 监测蓝牙与定位是否开启并跳转设置中开启_第1张图片
1208202-039ac6a0e29c3d06.png

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

#import       //添加定位服务头文件(不可缺少)    
@interface ViewController (){//添加代理协议 CLLocationManagerDelegate
    CLLocationManager *_locationManager;//定位服务管理类
}
 - (void)viewDidLoad
 {
     [super viewDidLoad];
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager = self;
    [_locationManager requestWhenInUseAuthorization];
   // [_locationManager requestAlwaysAuthorization];
}

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

iOS 监测蓝牙与定位是否开启并跳转设置中开启_第2张图片
1208202-b77244858ca6cd90.PNG

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

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

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

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    NSLog(@"授权状态改变");
if (status == kCLAuthorizationStatusDenied) {
        NSLog(@"定位权限没开启");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"打开定位,发现活动" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        UIAlertAction *confrmAction = [UIAlertAction actionWithTitle:@"打开" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:@"App-Prefs:root=LOCATION_SERVICES"];
            if ([[UIApplication sharedApplication]canOpenURL:url]) {
                [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
            }
            
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:confrmAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSString *errorString;
    NSLog(@"定位失败原因: %@",[error localizedDescription]);
}

5、手机设置中的界面

iOS 监测蓝牙与定位是否开启并跳转设置中开启_第3张图片
1208202-9a8799ead5601182.PNG

(二)、获取蓝牙权限

1、首先在build phase中添加CoreBluetooth.framework

#import    
@interface BlueToothState()
@property (nonatomic,strong)CBCentralManager *centralManager;
 -(void)viewDidLoad{
 // 蓝牙检测
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
}

2、实现蓝牙代理

#pragma mark - CLLocationManagerDelegate
//每次蓝牙状态改变都会回调这个函数
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    
    if(central.state == CBManagerStatePoweredOn)
    {
        NSLog(@"蓝牙设备开着");
    }
    else if (central.state == CBManagerStatePoweredOff)
    {
        NSLog(@"蓝牙设备关着");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"打开蓝牙,匹配活动" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        UIAlertAction *confrmAction = [UIAlertAction actionWithTitle:@"打开" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Bluetooth"];
            if ([[UIApplication sharedApplication]canOpenURL:url]) {
                [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
            }
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:confrmAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else {
        NSLog(@"该设备蓝牙未授权或者不支持蓝牙功能");
    }
}

你可能感兴趣的:(iOS 监测蓝牙与定位是否开启并跳转设置中开启)