Open The macOS Accessibility System Preference Dialog Box Automatically From A SwiftUI App
Update:
Change: App Sandbox
to NO
Then add this code to check the permissions and open the dialog box if you don't have permissions:
func checkPermissions() -> Bool {
let options : NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
return AXIsProcessTrustedWithOptions(options)
}
Then call checkPermissions()
when you need.
Endnotes
-
I was doing some looking through entitlements for the accessibility, but that's not where they are. It's the
AXIsProcessTrustedWithOptions
call that does the work. But, you have to turn off theApp Sandbox
in entitlements to get the dialog (which is called "Accessibility Access") to show up directly. -
For reference and to aid search, the full text of the dialog is "APP_NAME" would like to control this computer using accessibility features. Grant access to this application in Privacy & Security settings, located in System Settings
Then there are two buttons for "Open System Settings" and "Deny"
References
-
This is the post where I found that you need to turn off Sandbox. I'm new to mac app development. I don't really know what the implications of that are, but it's working for my auto typer
-
One of the answers that pointed to the solution
-
This is the thing that checks if you have permissions and opens the dialog box (once you're not in the sandbox) if you don't
-
Basically nothing on this page, but it's one of the parts you need to make the solution work.
-
This offers another possible way to do things where you don't pop the system dialog, but do it yourself. I'm not doing that and am sticking with the default behavior.
-
Another pointer to the sandbox being the blocker
-
This post talks about deleting derived data. At one point I was doing this which helped sometimes when the preview in Xcode would fight with the build. I'm not sure it's necessary anymore, but keeping it here for reference just in case.
-
Another example of how to open the system preference manually. I'm not using it right now, but it's a good reference
-
This is another function you can use to get if an app has accessibility permissions. It just returns true or false without popping open the dialog box if the answer if false.