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
I'm not aware of an API that's specifically for getting the screen recording permission status. Besides creating a CGDisplayStream
and checking for nil, the Advances in macOS Security WWDC presentation also mentioned that certain metadata from the CGWindowListCopyWindowInfo()
API will not be returned unless permission is granted. So something like this does seem to work, although it has the same issue of relying on implementation details of that function:
private func canRecordScreen() -> Bool {
guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: AnyObject]] else { return false }
return windows.allSatisfy({ window in
let windowName = window[kCGWindowName as String] as? String
return windowName != nil
})
}