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

(一)、获取定位权限

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

1208202-039ac6a0e29c3d06.png

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

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

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

1208202-b77244858ca6cd90.PNG

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

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)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、手机设置中的界面

1208202-9a8799ead5601182.PNG

(二)、获取蓝牙权限

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

#import <CoreBluetooth/CoreBluetooth.h>   
@interface BlueToothState()<CLLocationManagerDelegate>
@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(@"该设备蓝牙未授权或者不支持蓝牙功能");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,881评论 18 139
  • ios 判断用户是否开启权限---并跳转“系统设置” 1.判断 访问相册 或 相机 权限是否开启 2.检测是否开启...
    crzios123阅读 6,737评论 0 2
  • Guide to BluetoothSecurity原文 本出版物可免费从以下网址获得:https://doi.o...
    公子小水阅读 8,221评论 0 6
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,116评论 25 708
  • “等待着你 ~等待你慢慢的靠近我~陪着我长长的夜到尽头~等待着你 ~等待你默默凝望着我~告诉我你的未来属于我~除了...
    子非鱼_c842阅读 525评论 0 0