CBCentralManager iOS10和iOS9 [英] CBCentralManager iOS10 and iOS9

查看:140
本文介绍了CBCentralManager iOS10和iOS9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm migrating to iOS10 but I also need my code to run on iOS9. I'm using CoreBluetooth and CBCentralManagerDelegate. I can get my code to work for iOS10 however I need the fallback to work for iOS9 as well.

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。

As well as for .poweredOff and .poweredOn.

任何想法我怎么能得到它在这两种情况下工作?

Any ideas how I can get it to work in both cases?

推荐答案

我联系了Apple,并给出了以下回复(释义)。

I contacted Apple about this and was given the following response (paraphrasing).

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

Due to the changing nature of swift, the above implementation is not possible however you can use the rawValue of the enum as the state is identical between the two classes. Therefore the following will work for now:

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
        }
    }
}

这篇关于CBCentralManager iOS10和iOS9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆