Swift: 맥의 배터리 온도 얻기

3547 단어 SwiftmacOStech
import IOKit

func temperature() -> Double {
    let service: io_service_t = IOServiceGetMatchingService(
        kIOMasterPortDefault,
        IOServiceNameMatching("AppleSmartBattery")
    )
    let prop = IORegistryEntryCreateCFProperty(
        service,
        NSString(string: "Temperature") as CFString,
        kCFAllocatorDefault,
        0
    )
    let _ = IOObjectRelease(service)
    guard let value = prop?.takeUnretainedValue(),
          let temp = value as? Double
    else { return 0.0 }
    return temp / 100.0
}

Swift.print(temperature())

좋은 웹페이지 즐겨찾기