Created
November 21, 2019 22:38
-
-
Save chockenberry/164ab2d3dd76736f81c9e9eed63d81bf to your computer and use it in GitHub Desktop.
Detecting Screen Recording Permission in macOS Catalina
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment