作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法在 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/
我是一名优秀的程序员,十分优秀!