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

℡╲_俬逩灬. 提交于 2019-12-13 02:54:39

问题


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:


The Bluetooth power alert is only shown if Bluetooth is turned off from the Settings app. If you simply disable Bluetooth from the control center, then the dialog is not shown. Disabling Bluetooth from the control center disconnects any connected peripherals but does not completely power off the Bluetooth hardware.

You can display your own alert in response to a .poweredOff state in the centralManagerDidUpdateState delegate method. Unfortunately you cannot send the user directly to the Bluetooth settings in this case.

You could log a Radar with Apple regarding this behaviour, but I suspect that they will respond that the system is working as intended.



来源:https://stackoverflow.com/questions/55212803/how-to-show-an-alert-when-turn-off-bluetooth-in-swift-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!