How to show an alert when turn off bluetooth in Swift app?

时间:2019-03-17 23:01:57

标签: ios swift bluetooth cbcentralmanager uialertviewdelegate

The question could be duplicated, but I have tried a lot of code and it wasn't work. I have read about

CBCentralManagerOptionShowPowerAlertKey

option and the possibility to show an alert to go settings bluetooth option into iPhone for turn on on the application, but I haven't get works this.

If I set the key to true the key not appear, and when I change the value to false the key not works...

I'm using this code into my Swift application:

var bCentralManger: CBCentralManager!
self.bCentralManger = CBCentralManager(delegate: self, queue: DispatchQueue.main, options: [CBCentralManagerOptionShowPowerAlertKey: true])

Works this option in iOS? I'm using iOS 12 on my device. Have anybody this code working? Thanks!

1 个答案:

答案 0 :(得分:0)

仅当从 Settings 应用程序关闭了Bluetooth时,才会显示Bluetooth电源警报。如果仅从控制中心禁用蓝牙,则不会显示该对话框。从控制中心禁用蓝牙会断开所有连接的外围设备的连接,但不能完全关闭蓝牙硬件的电源。

您可以在.poweredOff委托方法中显示自己的警报以响应centralManagerDidUpdateState状态。不幸的是,在这种情况下,您无法直接将用户发送到蓝牙设置。

您可以就此行为向Apple记录Radar,但我怀疑他们会回答该系统按预期运行。