gpt4 book ai didi

swift - 使用 Swift 检测 MacBook 盖子的打开/关闭?

转载 作者:行者123 更新时间:2023-12-03 17:31:10 26 4
gpt4 key购买 nike

有没有办法在 Swift 中检测 MacBook 盖子打开/关闭事件?我搜索了很多,但没有找到快速的方法。

最佳答案

我通过在我的应用程序中执行终端命令成功解决了这个问题。这是代码:

func lidClosed() -> Bool {
let pipe = Pipe()
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["-c", "ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1"]
process.standardOutput = pipe
let fileHandle = pipe.fileHandleForReading
process.launch()
if(String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)?.contains("Yes") ?? false){
return true
}
return false
}

关于swift - 使用 Swift 检测 MacBook 盖子的打开/关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com