Salem
Salem

Reputation: 1162

CGPreflightScreenCaptureAccess always returns false in development builds

I'm writing an app for macOS (12.1 locally) in Xcode 13.2.1. I need screenshot access, and it seems like the easiest way to get permission for that (in macOS 11 and later) is to use CGRequestScreenCaptureAccess, and to check eligibility using CGPreflightScreenCaptureAccess:

https://stackoverflow.com/a/65423834/444912

My code essentially looks like this:

let hasScreenAccess = CGPreflightScreenCaptureAccess();
if (!hasScreenAccess) {
    CGRequestScreenCaptureAccess()
}

When I run a fresh build, a modal appears as expected: Screenshot of app requresting recording permissions

and I'm able to see my app appear in System Preferences, also as expected: Screenshot of app in Screen Recording permissions section of System Preferences

Enabling my app's permissions prompts me to quit my app and restart it. This restarts my production copy of my app, from the app store. However, if I quit that and re-run the same build in XCode, my build is still not granted permission to record the screen (even though it appears as enabled in System Preferences). How do I allow my app to have permissions to record my screen locally?

Upvotes: 1

Views: 2512

Answers (3)

hiroshi
hiroshi

Reputation: 7261

In System Preferences, it can be the production copy of your app. You may need to replace it with your debug build app.

  1. Delete it from System Preferences.
  2. Locate your debug build app in Finder and drag & drop it to System Preferences.

Your debug build app must be given the permission.

Upvotes: 0

Alex
Alex

Reputation: 567

We had a similar issue after renaming one of our apps which made use of the /usr/sbin/screencapture command line tool. The security prompt appeared every time a screen capture was triggered, no matter the activation status in System Preferences.

During development, the tccutil reset ScreenCapture {bundle identifier} command helped keep the permissions clean for subsequent testing.

Upvotes: 5

Salem
Salem

Reputation: 1162

I solved this in the process of writing this question, so I figured I'd share my answer. If I delete the copy of my app from my Applications folder, things seem to work. I assume that there is some sort of "claiming" process that goes.

It seems to work if I rename the copy of the app in my Applications folder, delete the screen recording in System Preferences, and then run my fresh build from Xcode.

Upvotes: 1

Related Questions