Created
November 4, 2021 13:48
-
-
Save HereOrCode/270859c71f90720d880ccb2474f4e7df to your computer and use it in GitHub Desktop.
Check Screen Record Permission
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
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, []) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment