cjb*_*tin 9 ios core-bluetooth swift ios9 ios10
所以我正在迁移到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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Enum case 'unauthorized' is not a member of type 'CBManagerState'
Run Code Online (Sandbox Code Playgroud)
在线上:
case CBCentralManagerState.unauthorized:
Run Code Online (Sandbox Code Playgroud)
以及.poweredOff和.poweredOn.
在这两种情况下我是如何让它工作的任何想法?
Pau*_*w11 13
您可以简单地省略枚举类型名称,只使用.value.这将在没有警告的情况下编译,并且可以在iOS 10及更早版本上运行,因为底层原始值是兼容的.
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
}
}
Run Code Online (Sandbox Code Playgroud)
我使用Swift 2.3(针对iOS 8及更高版本)在Xcode 8上解决了这个问题,方法是创建一个CBCentralManager旧的枚举类型的扩展属性CBCentralManagerState.我把它命名了centralManagerState.我指的是CBCentralManager.centralManagerState我曾经指的地方CBCentralManager.state.
extension CBCentralManager {
internal var centralManagerState: CBCentralManagerState {
get {
return CBCentralManagerState(rawValue: state.rawValue) ?? .Unknown
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然他们还没有发布代码,但我从这个论坛帖子中得到了这个想法.
我就此事联系了苹果公司,并得到了以下答复(释义)。
由于 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5577 次 |
| 最近记录: |