用于访问电池信息的轻量级多平台框架
电池
用于访问电池信息的轻量级多平台框架
特征
- 获取电池百分比、充电状态、低功耗模式状态
- 通过已发布的属性观察电池变化
- 多平台支持
安装
您可以通过 Swift Package Manager 将电池框架添加到您的项目中。 只需转到 File > Swift Packages > Add Package 依赖项,然后输入以下 URL:https://github.com/pmanot/Battery。
用法
要使用电池,首先,导入模块:
import Battery
然后,创建 Battery 类的实例:
let battery = Battery()
var cancellables: [AnyCancellable] = []
battery.$percentage.sink { percentage in
print("Battery percentage: \(percentage)")
}
.store(in: &cancellables)
battery.$state.sink { state in
print("Battery state: \(state)")
}
.store(in: &cancellables)
battery.$isLowPowerModeEnabled.sink { isEnabled in
print("Low power mode enabled: \(isEnabled)")
}
.store(in: &cancellables)
应用程序接口
Battery
The main class of the framework. Provides the following properties:
percentage
: The battery percentage as an integer (0-100)state
: The current state of the battery (BatteryState)isLowPowerModeEnabled
: A boolean indicating whether low power mode is enabled or not
BatteryState
An enum representing the state of the battery. It has the following cases:
charging
: The battery is currently chargingdischarging
: The battery is dischargingchargedAndPlugged
: The battery is fully charged and plugged inunknown
: The battery state is unknown
PowerSource
An enum representing the power source. It has the following cases:
powerAdapter
: The battery is connected to a power adapterbattery
: The battery is running on its own powerunknown
: The power source is unknown
License
Battery is available under the MIT license. See the LICENSE file for more info.