【问题标题】:Swift: Getting the current state of the authorization of the BluetoothSwift:获取蓝牙授权的当前状态
【发布时间】:2020-03-25 08:21:20
【问题描述】:

我目前正在我的应用程序上实施蓝牙权限。我使用CBManager.authorization 来获取授权的当前状态,但它仅适用于 ios 13.1。这是open var class authorization,还有一个var authorization。对于 iOS 13 或更低版本,我无法调用它们中的任何一个。这是有道理的,因为 iOS 12 没有蓝牙权限。ios 13.0 有一个,但它不能被调用,这很奇怪。我真的需要一个好的做法来检查权限的状态。

【问题讨论】:

  • 您可以在 iOS 13.0 上使用已弃用的 authorization 实例属性,在 iOS 13.1++ 上使用 class car + 使用 #available 检查

标签: ios swift bluetooth core-bluetooth ios-bluetooth


【解决方案1】:

您应该使用CBCentralManage。创建实例:

let manager = CBCentralManage(delegate: self, queue: nil, options: nil)

并像这样使用委托:

func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .unknown: <#code#>
        case .resetting: <#code#>
        case .unsupported: <#code#>
        case .unauthorized: <#code#>
        case .poweredOff: <#code#>
        case .poweredOn: <#code#>
        @unknown default: <#code#>
        }
}

假设您没有忘记更新.plist 文件以获取权限消息

【讨论】:

  • 这个方法我已经用过了。问题是,我在最新的 iOS 版本上工作,所以我没有真正测试它是否适用于 iOS 13。那么在分配 CBCentralManager 之前这是否有效?因为这就是我想要的,而且新方法适用于最新的 iOS 版本。顺便说一句,您实际上可以在 centralManagerDidUpdateState 方法中为 ios 13 使用 central.authorization。
猜你喜欢
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多