gpt4 book ai didi

macos - 检测 macOS Catalina 上的屏幕录制设置

转载 作者:行者123 更新时间:2023-12-03 16:01:54 24 4
gpt4 key购买 nike

检测用户是否启用此 API 的可靠方法是什么?

即使禁用屏幕录制 API,

CGWindowListCreateImage 也会返回有效对象。有多种可能的组合(kCGWindowListOptionInclusionWindowkCGWindowListOptionOnScreenBelowWindow),并且只有某些组合会返回 NULL。

- (CGImageRef)createScreenshotImage
{
NSWindow *window = [[self view] window];
NSRect rect = [window frame];

rect.origin.y = NSHeight([[window screen] frame]) - NSMaxY([window frame]);
CGImageRef screenshot = CGWindowListCreateImage(
rect,
kCGWindowListOptionIncludingWindow,
//kCGWindowListOptionOnScreenBelowWindow,
0,//(CGWindowID)[window windowNumber],
kCGWindowImageBoundsIgnoreFraming);//kCGWindowImageDefault
return screenshot;
}

唯一可靠的方法是通过 CGDisplayStreamCreate,这是有风险的,因为 Apple 每年都会更改隐私设置。

   - (BOOL)canRecordScreen
{
if (@available(macOS 10.15, *)) {
CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, nil, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
;
});
BOOL canRecord = stream != NULL;
if (stream) {
CFRelease(stream);
}
return canRecord;
} else {
return YES;
}
}

最佳答案

这里提出的所有解决方案都存在这样或那样的缺陷。问题的根源在于您对窗口的了解权限(通过窗口列表中的名称)与您对窗口的进程所有者(例如 WindowServer 和 Dock)的了解权限之间没有关联。您查看屏幕上像素的权限是两组稀疏信息的组合。

以下启发式涵盖了 macOS 10.15.1 起的所有情况:

BOOL canRecordScreen = YES;
if (@available(macOS 10.15, *)) {
canRecordScreen = NO;
NSRunningApplication *runningApplication = NSRunningApplication.currentApplication;
NSNumber *ourProcessIdentifier = [NSNumber numberWithInteger:runningApplication.processIdentifier];

CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSUInteger numberOfWindows = CFArrayGetCount(windowList);
for (int index = 0; index < numberOfWindows; index++) {
// get information for each window
NSDictionary *windowInfo = (NSDictionary *)CFArrayGetValueAtIndex(windowList, index);
NSString *windowName = windowInfo[(id)kCGWindowName];
NSNumber *processIdentifier = windowInfo[(id)kCGWindowOwnerPID];

// don't check windows owned by this process
if (! [processIdentifier isEqual:ourProcessIdentifier]) {
// get process information for each window
pid_t pid = processIdentifier.intValue;
NSRunningApplication *windowRunningApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
if (! windowRunningApplication) {
// ignore processes we don't have access to, such as WindowServer, which manages the windows named "Menubar" and "Backstop Menubar"
}
else {
NSString *windowExecutableName = windowRunningApplication.executableURL.lastPathComponent;
if (windowName) {
if ([windowExecutableName isEqual:@"Dock"]) {
// ignore the Dock, which provides the desktop picture
}
else {
canRecordScreen = YES;
break;
}
}
}
}
}
CFRelease(windowList);
}

如果未设置canRecordScreen,则需要设置某种对话框来警告用户他们只能看到菜单栏、桌面图片和应用程序自己的图片 window 。这是how we presented it在我们的应用程序中xScope .

是的,我仍然对这些保护措施是通过 little regard to usability 引入的感到痛苦。 .

关于macos - 检测 macOS Catalina 上的屏幕录制设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56597221/

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