CBCentralManager iOS10和iOS9

希巴汀

因此,我正在迁移到iOS10,但是我还需要我的代码才能在iOS9上运行。我正在使用CoreBluetooth和CBCentralManagerDelegate。我可以让我的代码适用于iOS10,但是我也需要后备才能适用于iOS9。

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if #available(iOS 10.0, *) {
        switch central.state{
        case CBManagerState.unauthorized:
            print("This app is not authorised to use Bluetooth low energy")
        case CBManagerState.poweredOff:
            print("Bluetooth is currently powered off.")
        case CBManagerState.poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
    } else {

        // Fallback on earlier versions
        switch central.state{
        case CBCentralManagerState.unauthorized:
            print("This app is not authorised to use Bluetooth low energy")
        case CBCentralManagerState.poweredOff:
            print("Bluetooth is currently powered off.")
        case CBCentralManagerState.poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
    }
}

我得到错误:

Enum case 'unauthorized' is not a member of type 'CBManagerState'

在线上:

case CBCentralManagerState.unauthorized: 

以及.poweredOff和.poweredOn。

有什么想法可以让我在两种情况下都能正常工作吗?

希巴汀

我就此事与苹果公司联系,并得到了以下答复(措辞)。

由于swift的变化性质,上述实现是不可能的,但是您可以使用枚举的rawValue,因为两个类之间的状态相同。因此,以下内容目前适用:

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if #available(iOS 10.0, *) {
        switch central.state{
        case CBManagerState.unauthorized:
            print("This app is not authorised to use Bluetooth low energy")
        case CBManagerState.poweredOff:
            print("Bluetooth is currently powered off.")
        case CBManagerState.poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
    } else {
        // Fallback on earlier versions
        switch central.state.rawValue {
        case 3: // CBCentralManagerState.unauthorized :
            print("This app is not authorised to use Bluetooth low energy")
        case 4: // CBCentralManagerState.poweredOff:
            print("Bluetooth is currently powered off.")
        case 5: //CBCentralManagerState.poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章