3

To be able to use certain features, users need to enable apps in accessibility panel. There are two ways to bring up the page shown as below

Solution 1

NSDictionary* options = @{static_cast<id> (kAXTrustedCheckOptionPrompt): @YES};
return AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef> (options));

Solution 2

NSString* urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility";
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:urlString]];

Compared to solution 1, solution 2 doesn't require the explaining dialog which introduces one extra click. And this is the main reason I prefer using solution 2.

But what I find is sometimes the app I enabled is not recognized by the system using solution 2. Because sometimes even if the app is enabled in accessibility panel, when I start the bundle which would spawn another child process, the standard error outputs something like "assitive device does not trust this process".

Does solution 1 do some hidden tricks while solution 2 doesn't?

Jerry
  • 1,704
  • 5
  • 20
  • 40

1 Answers1

5

To directly answer the question: I don't know the difference, but I also noticed that calling AXIsProcessTrustedWithOptions from 1st solution somehow makes my app appear in the accessibility pane, while 2nd solution doesn't. Magic? Maybe.

But there is a way to use the 1st solution without the dialog prompting user for another click.

Solution 3 (spinoff of 1st):

NSDictionary* options = @{static_cast<id> (kAXTrustedCheckOptionPrompt): @NO};
return !AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef> (options));

Somehow doing it this way doesn't cause the dialog to pop up; yet it triggers the magical AXIsProcessTrustedWithOptions, which makes the app appear in the accessibility pane.

You can then use the returned value to determine if you need to open the pane for the user, show some custom dialog, or anything else you want.

Source and more info: https://stackoverflow.com/a/18121292/8538394

karololszak
  • 88
  • 1
  • 6