Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CBCentralManager iOS10 and 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
        }
    }
}

I get the error:

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

On the line:

case CBCentralManagerState.unauthorized: 

As well as for .poweredOff and .poweredOn.

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

like image 613
cjbatin Avatar asked Sep 12 '16 12:09

cjbatin


1 Answers

You can simply omit the enumeration type name and just use the .value. This will compile without warnings and works on iOS 10 and earlier since the underlying raw values are compatible.

func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state{
        case .unauthorized:
            print("This app is not authorised to use Bluetooth low energy")
        case .poweredOff:
            print("Bluetooth is currently powered off.")
        case .poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
}
like image 186
Paulw11 Avatar answered Oct 20 '22 21:10

Paulw11