以下代码在Mojave中可以正常工作。
let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly, CGWindowListOption.optionOnScreenAboveWindow)
let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]
for entry in windowList!
{ let name = (entry[kCGWindowName as String] != nil) ? entry[kCGWindowName as String] as! String : ""
...
在卡塔琳娜州
entry[kCGWindowName as String]
总是零
在SO中:Detecting screen recording settings on macOS Catalina我读过,这不是启用“屏幕录像API”以及如何检测(如果已启用)所必需的。
很遗憾,我没有找到如何启用“屏幕录像API”。
如SO中所述:macOS Catalina screen recording permission我打开了自动代码签名。
在“系统偏好设置”中,我看不到添加应用程序,授予“屏幕录像”的“ +”。
如何授予屏幕录制的API权限?
答案 0 :(得分:3)
这是一个请求访问屏幕录制的低级 API。调用此函数将提示授予屏幕录制权限。
var popup_v = document.getElementById("myPopup");
function popup() {
popup_v.classList.toggle("show");
}
var trueorfalse = false;
document.addEventListener('click', function(event) {
if (trueorfalse === false) }
trueorfalse = true; // popup is there
popup_v.classList.toggle("show");
} else {
popup_v.classList.remove("show");
}
}
};
答案 1 :(得分:1)
您需要调用任何Screen Record API函数。例如屏幕截图:
(void)showScreenRecordingPrompt {
/*
macos 10.14 and lower do not require screen recording permission
to get window titles
*/
if(@available(macos 10.15, *)) {
/*
To minimize the intrusion just make a 1px image of the upper left corner
This way there is no real possibilty to access any private data
*/
CGImageRef screenshot = CGWindowListCreateImage(
CGRectMake(0, 0, 1, 1),
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID,
kCGWindowImageDefault);
CFRelease(screenshot);
}
解决方案是从这里获得的:https://blog.csdn.net/lovechris00/article/details/96979960