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)
}