Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何处理蓝牙权限_Swift - Fatal编程技术网

Swift 如何处理蓝牙权限

Swift 如何处理蓝牙权限,swift,Swift,那么,当我请求用户打开蓝牙时,我如何检查用户是否单击了“不允许”按钮。我想提供一个警报,当他们单击“不允许”时,告诉他们打开蓝牙。正如您所看到的,我尝试通过添加我创建的警报函数来实现这一点。但是,当我单击“不允许”时,它不会执行警报 代码 警觉的 func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .unauthorized

那么,当我请求用户打开蓝牙时,我如何检查用户是否单击了“不允许”按钮。我想提供一个警报,当他们单击“不允许”时,告诉他们打开蓝牙。正如您所看到的,我尝试通过添加我创建的警报函数来实现这一点。但是,当我单击“不允许”时,它不会执行警报

代码

警觉的

   func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .unauthorized:
            switch central.authorization {
            case .allowedAlways: break
            case .denied:
                showBluetoothSettings()
            case .restricted:
                print("restricted")
            case .notDetermined: break
            @unknown default:
                print("error")
            }
        case .unknown:
            print("Unkown")
        case .unsupported: break
        case .poweredOn:
            self.centralManager?.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey:true])
        case .poweredOff:
            print("powered on")
        case .resetting: break
        default:
            print("error")
            
            break
        }
    }
func showBluetoothSettings() {
    let alert = UIAlertController(title: "Bluetooth", message: "You need to turn on bluetooth",preferredStyle: UIAlertController.Style.alert)
    
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { _ in
        let bluetoothVC = BluetoothOnboardingVc()
        bluetoothVC.modalPresentationStyle = .fullScreen
        self.present(bluetoothVC,animated: true)
        self.centralManager?.stopScan()
        
    }))
    alert.addAction(UIAlertAction(title: "turn on",
                                  style: UIAlertAction.Style.default,
                                  handler: {(_: UIAlertAction!) in
                                    
                                    self.centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)
                                    self.centralManager?.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey:true])
                                    
                                  }))
    self.present(alert, animated: true, completion: nil)
}