title: Global events, the Mac App Store, and the sandbox
body: <p>I'm working on an app where using global key-down events will be a requirement for its operation. Additionally, I plan on distributing this strictly via the App Store. (It's a Mac app, not iOS.) I've gotten an example of listening for the global events working via addGlobalMonitorForEventsMatchingMask, but with caveats.</p>
  
  <p>Note: I am making the choice to use the modern API's and not rely on the earlier Carbon hotkey methods. In the event that they are deprecated eventually, I don't want to have to figure this problem out later.</p>
  
  <p>The principle issue is that the app has to be trusted in order for global events to be detected. Otherwise, accessibility has to be enabled for all apps. When I enable accessibility, events are detected successfully. This requirement is documented here, <a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/MonitoringEvents/MonitoringEvents.html" rel="nofollow noreferrer">https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/MonitoringEvents/MonitoringEvents.html</a>.</p>
  
  <p>I would prefer that for my users, they will not have to enable accessibility. From other research I've done, you can get an application to be trusted by calling AXMakeProcessTrusted, then restarting the application.</p>
  
  <p>In the code that I'm using, I do not get an authentication prompt. The app will restart, but is still not trusted (likely because I don't get an authentication prompt). Here's my code for this part:</p>
  
  <pre><code>- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  {
      if (!AXAPIEnabled() &amp;&amp; !AXIsProcessTrusted()) {
  
          NSString *appPath = [[NSBundle mainBundle] bundlePath];
          AXError error = AXMakeProcessTrusted( (CFStringRef)CFBridgingRetain(appPath) );
  
          [self restartApp];
      }
  }
  
  - (void)restartApp{
  
      NSTask *task = [[NSTask alloc] init];
      NSMutableArray *args = [NSMutableArray array];
      [args addObject:@"-c"];
      [args addObject:[NSString stringWithFormat:@"sleep %d; open \"%@\"", 3, [[NSBundle mainBundle] bundlePath]]];
      [task setLaunchPath:@"/bin/sh"];
      [task setArguments:args];
      [task launch];
      [NSApp terminate:nil];
  }
  </code></pre>
  
  <p>Further, I've looked at the documentation for Authorization Service Tasks here <a href="https://developer.apple.com/library/archive/documentation/Security/Conceptual/authorization_concepts/03authtasks/authtasks.html#//apple_ref/doc/uid/TP30000995-CH206-BCIGAIAG" rel="nofollow noreferrer">https://developer.apple.com/library/archive/documentation/Security/Conceptual/authorization_concepts/03authtasks/authtasks.html#//apple_ref/doc/uid/TP30000995-CH206-BCIGAIAG</a>.</p>
  
  <p>The first thing that worries me that pops out is this info box, "Important The authorization services API is not supported within an app sandbox because it allows privilege escalation."</p>
  
  <p>If this API is required to get the authentication prompt before restarting the app, it seems that I may <em>not</em> be able to get global events without the accessibility feature enabled.</p>
  
  <p>In summary, my specific questions are:</p>
  
  <ol>
  <li><p>Is there an error in my sample code about how to get the
  authentication prompt to appear?</p></li>
  <li><p>In order to get the authentication prompt to appear, am I required
  to use the Authorization Services API?</p></li>
  <li><p>Is it possible, or not possible, to have a sandboxed app that has
  access to global events?</p></li>
  </ol>