mica
mica

Reputation: 4308

Enabling Screen-Recording API in Catalina (kCGWindowName)

The following code worked fine in Mojave .

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly, CGWindowListOption.optionOnScreenAboveWindow)
let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]

for entry  in windowList!
{ let name = (entry[kCGWindowName as String] != nil) ? entry[kCGWindowName as String] as! String : ""
  ...

In Catalina

entry[kCGWindowName as String] 

always is nil

In SO: Detecting screen recording settings on macOS Catalina I read, that is´t required to enable the "screen recording API" an how to detect, if it's enabled.

Unfortunately I do not find out, how to enable the "screen recording API".

As mentioned in SO: macOS Catalina screen recording permission I switched on Automatic Code Signing.

In the System Preferences I see no "+" to add an App, to grant "screen recording".

How can the API Permission for Screen Recording be granted?

Upvotes: 6

Views: 3082

Answers (2)

Somesh Karthik
Somesh Karthik

Reputation: 546

This a low level api to request access to screen recording. Calling this functions will present a prompt to give access for screen recording.

/* Requests event listening access if absent, potentially prompting */
@available(macOS 10.15, *)
public func CGRequestScreenCaptureAccess() -> Bool

Upvotes: 5

xpert13
xpert13

Reputation: 21

You need to call any Screen Record API function. For example screenshot:

(void)showScreenRecordingPrompt {
  /*
   macos 10.14 and lower do not require screen recording permission 
   to get window titles 
  */
  if(@available(macos 10.15, *)) {
    /*
     To minimize the intrusion just make a 1px image of the upper left corner
     This way there is no real possibilty to access any private data
    */
    CGImageRef screenshot = CGWindowListCreateImage(
        CGRectMake(0, 0, 1, 1),
        kCGWindowListOptionOnScreenOnly,
        kCGNullWindowID,
        kCGWindowImageDefault);

    CFRelease(screenshot);

}

The solution was get from here: https://blog.csdn.net/lovechris00/article/details/96979960

Upvotes: 2

Related Questions