Detecting screen recording settings on macOS Catalina

后端 未结 7 1413
清歌不尽
清歌不尽 2020-11-30 23:01

What\'s is a reliable way to detect if user has enabled this API?

CGWindowListCreateImage returns a valid object even if screen recording API is disable

相关标签:
7条回答
  • 2020-11-30 23:59

    The most favorable answer is not exactly right, he left out some sences, like sharing state.

    we can find the answer in WWDC(https://developer.apple.com/videos/play/wwdc2019/701/?time=1007)

    Here are some excerpts from WWDC: the window name and sharing state are not available, unless the user has preapproved the app for screen recording. And this is because some apps put sensitive data such as account names or more likely web page URLs in the window's name.

    - (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;
    }
    
    0 讨论(0)
提交回复
热议问题