快速检查Mac电池百分比

时间:2015-03-26 12:54:08

标签: macos swift

我一直在尝试以编程方式检查mac电池级别。可以在ios上完成但是我想在mac.i中找到一些在stackoverflow上找到的资源但这些链接已被弃用。任何想法?

4 个答案:

答案 0 :(得分:3)

首先创建一个" Umbrella-Bridging-Header.h" 内容:

#import <IOKit/ps/IOPowerSources.h>

然后在main.swift

import Foundation

println("Hello, World!")
let timeRemaining = IOPSGetTimeRemainingEstimate ()
println("timeRemaining: \(timeRemaining)")

答案 1 :(得分:1)

首先,您可以看到关于如何在快速项目中包含Objective-C代码的答案here(非常好的帖子顺便说一句)。

然后,查看IOMPowerSource课程。它应该包括报告计算机电源信息状态所需的一切。

答案 2 :(得分:1)

如果您不想添加Objective-C桥接,您只需知道几个值。然后你可以使用这个功能。

func getBatteryState() -> [String?]
{
    let task = Process()
    let pipe = Pipe()
    task.launchPath = "/usr/bin/pmset"
    task.arguments = ["-g", "batt"]
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String

    let batteryArray = output.components(separatedBy: ";")
    let source = output.components(separatedBy: "'")[1]
    let state = batteryArray[1].trimmingCharacters(in: NSCharacterSet.whitespaces).capitalized
    let percent = String.init(batteryArray[0].components(separatedBy: ")")[1].trimmingCharacters(in: NSCharacterSet.whitespaces).characters.dropLast())
    var remaining = String.init(batteryArray[2].characters.dropFirst().split(separator: " ")[0])
    if(remaining == "(no"){
        remaining = "Calculating"
    }
    return [source, state, percent, remaining]
}

print(getBatteryState().flatMap{$0}) -> "AC Power", "Discharging", "94", "3:15"

pmset是一个非常古老的命令行函数,将来不太可能改变。当然,这并没有提供像mAh这样的电源选项的扩展属性,但这对我来说已经足够了,因为我只需要知道它是否充电以及当前电池的百分比。

只需2美分。我理解人们是否会发现这种令人沮丧的使用方式。

N.B。如果充电 - 剩余时间将显示充满电的时间。 如果放电 - 它会显示它排出的时间。

答案 3 :(得分:1)

Swift 2 @Just A Minnion

答案的版本
import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var label: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        label.stringValue = String(getBatteryState().flatMap{$0})
    }

    func getBatteryState() -> [String?] {     
        let task = NSTask()
        let pipe = NSPipe()

        task.launchPath = "/usr/bin/pmset"
        task.arguments = ["-g", "batt"]
        task.standardOutput = pipe
        task.launch()

        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        let output = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
        let batteryArray = output.componentsSeparatedByString(";")
        let source = output.componentsSeparatedByString("'")[1]
        let state = batteryArray[0].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).capitalizedString
        let percent = String.init(batteryArray[0].componentsSeparatedByString(")")[0].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()))
        var remaining = String.init(batteryArray[0].characters.dropFirst().split(" ")[1])

        if (remaining == "(no") {
            remaining = "Calculating"
        }
        return [source, state, percent, remaining]
    }
}