Detecting screen recording settings on macOS Catalina

后端 未结 7 1523
清歌不尽
清歌不尽 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:47

    As of Nov19 chockenberry has correct answer.

    As @onelittlefish pointed out the kCGWindowName is being omitted in case user has not enabled the screen recording access in privacy pane. This method also doesn't trigger the privacy alert.

    - (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;
    }
    

提交回复
热议问题