26

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

CGWindowListCreateImage即使屏幕录制 API 被禁用,也会返回一个有效的对象。可能有多种组合 ( kCGWindowListOptionIncludingWindow, kCGWindowListOptionOnScreenBelowWindow),只有一些会返回 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;
        }
    }
4

9 回答 9

26

这里介绍的所有解决方案都以某种方式存在缺陷。问题的根源在于您了解窗口的权限(通过窗口列表中的名称)与您了解窗口的进程所有者(例如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未设置,则需要设置某种对话框,警告用户他们只能看到菜单栏、桌面图片和应用程序自己的窗口。这是我们在应用程序xScope中呈现它的方式。

是的,我仍然很遗憾引入这些保护措施时很少考虑可用性

于 2019-11-21T23:05:25.133 回答
13

Apple 提供了直接的低级 api 来检查访问和授予访问权限。无需使用棘手的解决方法。

/* Checks whether the current process already has screen capture access */
@available(macOS 10.15, *)
public func CGPreflightScreenCaptureAccess() -> Bool

使用上述功能检查屏幕捕获访问。

如果未授予访问权限,请使用以下功能提示访问权限

/* Requests event listening access if absent, potentially prompting */
@available(macOS 10.15, *)
public func CGRequestScreenCaptureAccess() -> Bool

从文档中截取的屏幕截图

于 2020-12-23T11:49:29.557 回答
4

@marek-h 发布了一个很好的示例,可以检测屏幕录制设置而不显示隐私警报。顺便说一句,@jordan-h 提到当应用程序通过 beginSheetModalForWindow 显示警报时,此解决方案不起作用。

我发现 SystemUIServer 进程总是创建一些带有名称的窗口:AppleVolumeExtra、AppleClockExtra、AppleBluetoothExtra ...

在隐私首选项中启用屏幕录制之前,我们无法获取这些窗口的名称。而当我们至少能得到其中一个名字的时候,就说明用户开启了录屏功能。

所以我们可以检查窗口的名称(由 SystemUIServer 进程创建)来检测屏幕录制偏好,它在 macOS Catalina 上运行良好。

#include <AppKit/AppKit.h>
#include <libproc.h>

bool isScreenRecordingEnabled()
{
    if (@available(macos 10.15, *)) {
        bool bRet = false;
        CFArrayRef list = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
        if (list) {
            int n = (int)(CFArrayGetCount(list));
            for (int i = 0; i < n; i++) {
                NSDictionary* info = (NSDictionary*)(CFArrayGetValueAtIndex(list, (CFIndex)i));
                NSString* name = info[(id)kCGWindowName];
                NSNumber* pid = info[(id)kCGWindowOwnerPID];
                if (pid != nil && name != nil) {
                    int nPid = [pid intValue];
                    char path[PROC_PIDPATHINFO_MAXSIZE+1];
                    int lenPath = proc_pidpath(nPid, path, PROC_PIDPATHINFO_MAXSIZE);
                    if (lenPath > 0) {
                        path[lenPath] = 0;
                        if (strcmp(path, "/System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/SystemUIServer") == 0) {
                            bRet = true;
                            break;
                        }
                    }
                }
            }
            CFRelease(list);
        }
        return bRet;
    } else {
        return true;
    }
}
于 2019-11-10T06:21:50.987 回答
1

我不知道专门用于获取屏幕录制权限状态的 API。除了创建CGDisplayStream和检查 nil之外, macOS 安全性 WWDC 演示中的进步还提到,CGWindowListCopyWindowInfo()除非获得许可,否则不会返回 API 中的某些元数据。所以这样的事情似乎确实有效,尽管它具有依赖该函数的实现细节的相同问题:

private func canRecordScreen() -> Bool {
    guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: AnyObject]] else { return false }
    return windows.allSatisfy({ window in
        let windowName = window[kCGWindowName as String] as? String
        return windowName != nil
    })
}
于 2019-08-05T23:17:58.317 回答
1

截至 Nov19 chockenberry有正确答案。

正如@onelittlefish 指出的那样kCGWindowName,如果用户没有在隐私窗格中启用屏幕录制访问,则会省略。此方法也不会触发隐私警报。

- (BOOL)canRecordScreen
{
    if (@available(macOS 10.15, *)) {
        CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
        NSUInteger numberOfWindows = CFArrayGetCount(windowList);
        NSUInteger numberOfWindowsWithName = 0;
        for (int idx = 0; idx < numberOfWindows; idx++) {
            NSDictionary *windowInfo = (NSDictionary *)CFArrayGetValueAtIndex(windowList, idx);
            NSString *windowName = windowInfo[(id)kCGWindowName];
            if (windowName) {
                numberOfWindowsWithName++;
            } else {
                //no kCGWindowName detected -> not enabled
                break; //breaking early, numberOfWindowsWithName not increased
            }

        }
        CFRelease(windowList);
        return numberOfWindows == numberOfWindowsWithName;
    }
    return YES;
}
于 2019-08-06T15:17:31.720 回答
1

最有利的答案并不完全正确,他遗漏了一些感觉,比如分享状态。

我们可以在 WWDC( https://developer.apple.com/videos/play/wwdc2019/701/?time=1007)中找到答案

以下是 WWDC 的一些摘录:窗口名称和共享状态不可用,除非用户已预先批准该应用程序进行屏幕录制。这是因为某些应用程序将敏感数据(例如帐户名称或更可能的网页 URL)放在窗口名称中。

- (BOOL)ScreeningRecordPermissionCheck {
    if (@available(macOS 10.15, *)) {
        CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
        NSUInteger numberOfWindows = CFArrayGetCount(windowList);
        NSUInteger numberOfWindowsWithInfoGet = 0;
        for (int idx = 0; idx < numberOfWindows; idx++) {

            NSDictionary *windowInfo = (NSDictionary *)CFArrayGetValueAtIndex(windowList, idx);
            NSString *windowName = windowInfo[(id)kCGWindowName];
            NSNumber* sharingType = windowInfo[(id)kCGWindowSharingState];

            if (windowName || kCGWindowSharingNone != sharingType.intValue) {
                numberOfWindowsWithInfoGet++;
            } else {
                NSNumber* pid = windowInfo[(id)kCGWindowOwnerPID];
                NSString* appName = windowInfo[(id)kCGWindowOwnerName];
                NSLog(@"windowInfo get Fail pid:%lu appName:%@", pid.integerValue, appName);
            }
        }
        CFRelease(windowList);
        if (numberOfWindows == numberOfWindowsWithInfoGet) {
            return YES;
        } else {
            return NO;
        }
    }
    return YES;
}
于 2020-01-06T03:42:29.283 回答
1

从 MacOS 10.15.7 开始,获取可见窗口的窗口名称的启发式方法并不总是有效。有时我们只是找不到我们可以查询的有效窗口,并且会错误地推断我们没有权限。

但是,我找到了另一种直接查询(使用 sqlite)Apple TCC 数据库的方法——权限被持久化的模型。屏幕录制权限可在“系统级”TCC 数据库(位于/Library/Application Support/com.apple.TCC/TCC.db)中找到。如果您使用 sqlite 打开数据库,然后查询:SELECT allowed FROM access WHERE client="com.myCompany.myApp" AND service="kTCCServiceScreenCapture"您将得到答案。

与其他答案相比有两个缺点:

  • 要打开此 TCC.db 数据库,您的应用程序必须具有“全盘访问”权限。它不需要以“root”权限运行,如果您没有“全磁盘访问”权限,root 权限将无济于事。
  • 运行大约需要 15 毫秒,这比查询窗口列表要慢。

好的一面——它是对实际事物的直接查询,不依赖于任何窗口或查询时存在的进程。

这是一些执行此操作的草稿代码:

NSString *client = @"com.myCompany.myApp";
sqlite3 *tccDb = NULL;
sqlite3_stmt *statement = NULL;

NSString *pathToSystemTCCDB = @"/Library/Application Support/com.apple.TCC/TCC.db";
const char *pathToDBFile = [pathToSystemTCCDB fileSystemRepresentation];
if (sqlite3_open(pathToDBFile, &tccDb) != SQLITE_OK)
   return nil;
    
const char *query = [[NSString stringWithFormat: @"SELECT allowed FROM access WHERE client=\"%@\" AND service=\"kTCCServiceScreenCapture\"",client] UTF8String];
if (sqlite3_prepare_v2(tccDb, query , -1, &statement, nil) != SQLITE_OK)
   return nil;
    
BOOL allowed = NO;
while (sqlite3_step(statement) == SQLITE_ROW)
    allowed |= (sqlite3_column_int(statement, 0) == 1);

if (statement)
    sqlite3_finalize(statement);

if (tccDb)
    sqlite3_close(tccDb);

return @(allowed);

}

于 2020-11-03T14:35:35.777 回答
0

为我工作。代码来自:https ://gist.github.com/code4you2021/270859c71f90720d880ccb2474f4e7df

import Cocoa

struct ScreenRecordPermission {
    static var hasPermission: Bool {
        permissionCheck()
    }

    static func permissionCheck() -> Bool {
        if #available(macOS 10.15, *) {
            let runningApplication = NSRunningApplication.current
            let processIdentifier = runningApplication.processIdentifier
            guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID)
                as? [[String: AnyObject]],
                let _ = windows.first(where: { window -> Bool in
                    guard let windowProcessIdentifier = (window[kCGWindowOwnerPID as String] as? Int).flatMap(pid_t.init),
                          windowProcessIdentifier != processIdentifier,
                          let windowRunningApplication = NSRunningApplication(processIdentifier: windowProcessIdentifier),
                          windowRunningApplication.executableURL?.lastPathComponent != "Dock",
                          let _ = window[String(kCGWindowName)] as? String
                    else {
                        return false
                    }

                    return true
                })
            else {
                return false
            }
        }
        return true
    }

    static func requestPermission() {
        if #available(macOS 10.15, *) {
            CGWindowListCreateImage(CGRect(x: 0, y: 0, width: 1, height: 1), .optionOnScreenOnly, kCGNullWindowID, [])
        }
    }
}

# how to use
# print("hasPermission: ", ScreenRecordPermission.hasPermission)
于 2021-11-04T13:51:09.327 回答
-1

上面的答案不能正常工作。下面是正确答案。

private var canRecordScreen : Bool {
    guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: AnyObject]] else { return false }
    return windows.allSatisfy({ window in
        let windowName = window[kCGWindowName as String] as? String
        let isSharingEnabled = window[kCGWindowSharingState as String] as? Int
        return windowName != nil || isSharingEnabled == 1
    })
  }
于 2019-11-22T10:09:35.267 回答