Ios 你能得到苹果手表的大概电池百分比吗?
Apple Watch是否有一个对象或功能可以访问其大致的电池寿命?这应该可以工作:Ios 你能得到苹果手表的大概电池百分比吗?,ios,swift,ios8,apple-watch,Ios,Swift,Ios8,Apple Watch,Apple Watch是否有一个对象或功能可以访问其大致的电池寿命?这应该可以工作:UIDevice.currentDevice().batteryLevel这将返回一个浮点值根据Sean的说法,在当前WatchKit版本中,您无法获取Apple Watch的电池电量,下面的代码只是返回iPhone电池电量: 斯威夫特: var x = UIDevice.currentDevice().batteryLevel 目标C: int x = [UIDevice currentDevice].bat
UIDevice.currentDevice().batteryLevel
这将返回一个浮点值根据Sean的说法,在当前WatchKit版本中,您无法获取Apple Watch的电池电量,下面的代码只是返回iPhone电池电量:
斯威夫特:
var x = UIDevice.currentDevice().batteryLevel
目标C:
int x = [UIDevice currentDevice].batteryLevel;
这是app Store上电池顾问Apple Watch应用程序(由Applestan提供)中使用的代码。这是可能的,但仅在WatchOS 4发布后。UIDevice仅适用于iOS设备,因此与此问题绝对无关。与之相对应的是WatchOS中的WKInterfaceDevice 正确的解决方案是: OBJECTIVE-C版本 但您必须在以下情况下允许:
[WKInterfaceDevice currentDevice].batteryMonitoringEnabled = YES;
SWIFT 4.2版本:
要使其可用,您必须使用:
WKInterfaceDevice.current().isBatteryMonitoringEnabled = true
Swift(WatchKit 4.x)版本。注意:必须设置isBatteryMonitoringEnabled=true,否则将无法获得有效结果。在我的例子中,我不关心后续的通知,所以我只是立即设置为false。另请注意,当前模拟器不模拟batteryLevel/batteryState
let curDevice = WKInterfaceDevice.current()
curDevice.isBatteryMonitoringEnabled = true // Enable just long enough for data
let chargeLevel = curDevice.batteryLevel
let chargeState = curDevice.batteryState
curDevice.isBatteryMonitoringEnabled = false
此方法返回iPhone/iPad设备的电池电量,而不是iWatch的电池电量。在目前的WatchKit版本中,无法获得手表的电池电量。我在模拟器上测试了它,手表和iPhone的电池电量都为-1,但我不能确定它在实际的苹果手表上是否工作。我仍然认为这是一个可能的解决方案,直到他们在WKInterfaceDevice中添加电池电量。我想我可以在真正的iPhone上测试我的应用程序,然后看看真正的iPhone电池与apple watch模拟器的电池是否匹配或不同。
WKInterfaceDevice.current().isBatteryMonitoringEnabled = true
let curDevice = WKInterfaceDevice.current()
curDevice.isBatteryMonitoringEnabled = true // Enable just long enough for data
let chargeLevel = curDevice.batteryLevel
let chargeState = curDevice.batteryState
curDevice.isBatteryMonitoringEnabled = false