你能获得Apple Watch的大概电池百分比吗?

问题描述 投票:3回答:4

Apple Watch是否有可以访问其大致电池寿命的对象或功能?

ios swift ios8 apple-watch
4个回答
3
投票

这是可能的,但仅限于WatchOS 4的发布.UIDevice仅适用于iOS设备,因此它绝对与此问题无关。它的对应部分是WatchOS中的WKInterfaceDevice。

正确的解决方案是:

OBJECTIVE-C版本

float *watchBatteryPercentage = [WKInterfaceDevice currentDevice].batteryLevel;

但你必须先允许它:

[WKInterfaceDevice currentDevice].batteryMonitoringEnabled = YES;

SWIFT 4.2版本:

var watchBatteryPercentage:Int {
    return Int(roundf(WKInterfaceDevice.current().batteryLevel * 100))
}

为了使它可用,你必须使用:

WKInterfaceDevice.current().isBatteryMonitoringEnabled = true

2
投票

根据Sean的说法,你无法在当前的WatchKit版本中获得Apple Watch电池级别,以下代码只返回iPhone电池电量:

迅速:

var x = UIDevice.currentDevice().batteryLevel

Objective-C的:

int x = [UIDevice currentDevice].batteryLevel;

这是App Store上的Battery Adviser Apple Watch应用程序(Applestan)中使用的代码。


1
投票

Swift(WatchKit 4.x)版本。请注意,您必须设置isBatteryMonitoringEnabled = true,否则您将无法获得有效结果。在我的情况下,我不关心后续通知,所以我立即回到假。另请注意,当前模拟器不会模拟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

-3
投票

这应该工作:UIDevice.currentDevice().batteryLevelthis将返回Float

© www.soinside.com 2019 - 2024. All rights reserved.