我已经将NSBluetoothPeripheralUsageDescription
键添加到info.plist
文件中,就像NSLocationUsageDescription
键一样,但是当我在代码中初始化CBCentralManager
时,它不会弹出警报视图以在NSBluetoothPeripheralUsageDescription
键中显示消息。
例如,当我们在 info.plist
中添加 NSLocationUsageDescription
键时,当应用程序尝试使用您的位置信息时,它会弹出一个警报视图,但它不能作为NSBluetoothPeripheralUsageDescription
键工作。为什么?
在 xcode 项目的 info.plist 中添加一个键值
隐私-蓝牙始终使用说明:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app need your bluetooth</string>
当你初始化CBCentralManager时,参数选项应该包含CBCentralManagerOptionShowPowerAlertKey
let dict = [CBCentralManagerOptionShowPowerAlertKey:true]
//if init the manager in sub thread queue, when u got some event, u need to update UI in the main queue
//entralManager = CBCentralManager(delegate: self, queue: centralInitQueue, options: dict)
// the queue paramter is nil, manager will dispatches central role events using the main queued
centralManager = CBCentralManager(delegate: self, queue: nil, options: dict)
仅当您在Info.plist
UIBackgroundModes
中bluetooth-central
或bluetooth-peripheral
之一时才需要NSBluetoothPeripheralUsageDescription
:
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
</array>