CGWindowListCopyWindowInfo returns kCGWindowSharingState 设置为 0 的信息

CGWindowListCopyWindowInfo returns info with kCGWindowSharingState set to 0

我正在尝试从另一个进程获取 window 的 kCGWindowSharingState 值。

我创建了一个新项目并将 NSWindow.sharingType 设置为 NSWindowSharingReadWrite

我创建了另一个项目,我从那个项目中 运行 以下代码获取 kCGWindowSharingState 的值:

CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSLog(@"windowList: %@", windowList);

它 returns 具有 kCGWindowSharingState 值的字典数组,但它始终设置为 0

任何关于此的帮助都会很好。提前致谢。

您的另一个项目必须具有屏幕录制权限。是否有 API 要求他们?不行。您必须尝试录制屏幕才能获取系统对话框。

- (BOOL)doWeHaveScreenRecordingPermissions {
    // Try to record -> triggers system dialog
    CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, nil, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
    });

    // NULL = No permissions
    BOOL result = stream != NULL;

    if (stream) {
        CFRelease(stream);
    }

    return result;
}

您可以在没有这些权限的情况下使用 CGWindowListCopyWindowInfo,但 kCGWindowSharingState 的值将始终为 0。一旦您的另一个应用获得这些权限,将返回正确的值。

WWDC 2019 - Advances in macOS Security 中简要讨论。

此安全舞蹈适用于 kCGWindowSharingState & kCGWindowName


我的申请代表:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSLog(@"Screen Recording permissions: %@", [self doWeHaveScreenRecordingPermissions] ? @"Yay!" : @"N/A");

    CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
    if (windowList == NULL) {
        return;
    }

    CFIndex count = CFArrayGetCount(windowList);
    for (CFIndex index = 0 ; index < count ; index++) {
        CFDictionaryRef windowInfo = CFArrayGetValueAtIndex(windowList, index);

        CFStringRef owner = CFDictionaryGetValue(windowInfo, kCGWindowOwnerName);
        if (owner == NULL) {
            continue;
        }

        if (CFStringCompare(owner, CFSTR("ShareWindow"), 0) != kCFCompareEqualTo) {
            continue;
        }

        NSLog(@"Got a window from ShareWindow! %@", windowInfo);
    }

    CFRelease(windowList);
}

没有屏幕录制权限

2020-06-03 13:22:36.073581+0200 GetSharedWindow[54889:828225] Screen Recording permissions: N/A
2020-06-03 13:22:36.074951+0200 GetSharedWindow[54889:828225] Got a window from ShareWindow! {
    kCGWindowAlpha = 1;
    kCGWindowBounds =     {
        Height = 292;
        Width = 480;
        X = 235;
        Y = 538;
    };
    kCGWindowIsOnscreen = 1;
    kCGWindowLayer = 0;
    kCGWindowMemoryUsage = 1152;
    kCGWindowNumber = 5672;
    kCGWindowOwnerName = ShareWindow;
    kCGWindowOwnerPID = 54445;
    kCGWindowSharingState = 0;         <-----
    kCGWindowStoreType = 1;
}

屏幕录制权限

2020-06-03 13:24:33.823247+0200 GetSharedWindow[55697:832599] Screen Recording permissions: Yay!
2020-06-03 13:24:33.824580+0200 GetSharedWindow[55697:832599] Got a window from ShareWindow! {
    kCGWindowAlpha = 1;
    kCGWindowBounds =     {
        Height = 292;
        Width = 480;
        X = 235;
        Y = 538;
    };
    kCGWindowIsOnscreen = 1;
    kCGWindowLayer = 0;
    kCGWindowMemoryUsage = 1152;
    kCGWindowName = Window;
    kCGWindowNumber = 5672;
    kCGWindowOwnerName = ShareWindow;
    kCGWindowOwnerPID = 54445;
    kCGWindowSharingState = 2;         <-----
    kCGWindowStoreType = 1;
}