NSBluetooth外围设备用法说明键字符串不弹出警报视图



我已经将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 UIBackgroundModesbluetooth-centralbluetooth-peripheral之一时才需要NSBluetoothPeripheralUsageDescription

<key>UIBackgroundModes</key>
<array>
   <string>bluetooth-central</string>
</array>

最新更新