全球活动,Mac App Store和沙盒

Geu*_*uis 6 cocoa sandbox objective-c mac-app-store

我正在开发一个应用程序,其中使用全局按键事件将是其操作的必要条件.此外,我计划通过App Store严格分发.(它是一个Mac应用程序,而不是iOS.)我已经得到了一个通过addGlobalMonitorForEventsMatchingMask监听全局事件的示例,但有一些警告.

注意:我正在选择使用现代API而不依赖于早期的Carbon热键方法.如果他们最终被弃用,我不想在以后解决这个问题.

主要问题是必须信任应用程序才能检测到全局事件.否则,必须为所有应用启用辅助功能.启用辅助功能时,会成功检测到事件.此要求记录在此处,http://developer.apple.com/library/mac/#DOCUMENTATION/Cocoa/Conceptual/EventOverview/MonitoringEvents/MonitoringEvents.html.

我希望对我的用户来说,他们不必启用辅助功能.从我已经完成的其他研究中,您可以通过调用AXMakeProcessTrusted来获取可信任的应用程序,然后重新启动应用程序.

在我正在使用的代码中,我没有得到身份验证提示.应用程序将重新启动,但仍然不受信任(可能是因为我没有收到身份验证提示).这是我的这部分代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    if (!AXAPIEnabled() && !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];
}
Run Code Online (Sandbox Code Playgroud)

此外,我在这里查看了授权服务任务的文档https://developer.apple.com/library/mac/#documentation/security/conceptual/authorization_concepts/03authtasks/authtasks.html#//apple_ref/doc/uid/TP30000995-CH206-BCIGAIAG.

弹出的第一件事就是弹出这个信息框,"重要的是,应用程序沙箱中不支持授权服务API,因为它允许权限提升."

如果在重新启动应用程序之前需要此API来获取身份验证提示,则似乎在启用辅助功能的情况下我可能无法获取全局事件.

总之,我的具体问题是:

  1. 我的示例代码中是否有关于如何显示身份验证提示的错误?

  2. 为了显示身份验证提示,我是否需要使用Authorization Services API?

  3. 是否有可能或不可能拥有一个可以访问全局事件的沙盒应用程序?

fik*_*nik 5

首先,您无法自动允许应用程序使用可在沙盒环境中工作的辅助功能API,因此也可以在应用商店中使用.推荐的方法是简单地引导用户,以便他们自己轻松启用它.新的API调用AXIsProcessTrustedWithOptions正是为此:

        NSDictionary *options = @{(id) kAXTrustedCheckOptionPrompt : @YES};
        AXIsProcessTrustedWithOptions((CFDictionaryRef) options);
Run Code Online (Sandbox Code Playgroud)

现在,关于你的第一个和第二个问题(仅仅为了完整性 - 再次它在沙盒中不起作用):背后的想法AXMakeProcessTrusted是你实际上创建了一个新的辅助应用程序,你从主应用程序以root身份运行.然后,该实用程序调用AXMakeProcessTrusted传入主应用程序的可执行文件.最后,您必须重新启动主应用程序.OSX 10.9中已弃用API调用.

要以root身份生成新进程,必须launchd使用SMJobSubmit.这将提示用户提供身份验证提示,说明应用程序正在尝试安装帮助程序工具以及是否应该允许它.具体来说:

    + (BOOL)makeTrustedWithError:(NSError **)error {
        NSString *label = FMTStr(@"%@.%@", kShiftItAppBundleId, @"mktrusted");
        NSString *command = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"mktrusted"];
        AuthorizationItem authItem = {kSMRightModifySystemDaemons, 0, NULL, 0};
        AuthorizationRights authRights = {1, &authItem};
        AuthorizationFlags flags = kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
        AuthorizationRef auth;

        if (AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &auth) == errAuthorizationSuccess) {
           // this is actually important - if from any reason the job was not removed, it won't relaunch
           // to check for the running jobs use: sudo launchctl list
           // the sudo is important since this job runs under root
           SMJobRemove(kSMDomainSystemLaunchd, (CFStringRef) label, auth, false, NULL);
           // this is actually the launchd plist for a new process
           // https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist
           NSDictionary *plist = @{
                   @"Label" : label,
                   @"RunAtLoad" : @YES,
                   @"ProgramArguments" : @[command],
                   @"Debug" : @YES
           };
           BOOL ret;
           if (SMJobSubmit(kSMDomainSystemLaunchd, (CFDictionaryRef) plist, auth, (CFErrorRef *) error)) {
               FMTLogDebug(@"Executed %@", command);
               ret = YES;
           } else {
               FMTLogError(@"Failed to execute %@ as priviledged process: %@", command, *error);
               ret = NO;
           }
           // From whatever reason this did not work very well
           // seems like it removed the job before it was executed
           // SMJobRemove(kSMDomainSystemLaunchd, (CFStringRef) label, auth, false, NULL);
           AuthorizationFree(auth, 0);
           return ret;
        } else {
           FMTLogError(@"Unable to create authorization object");
           return NO;
        }
    }
Run Code Online (Sandbox Code Playgroud)

至于重新启动,通常还使用外部实用程序来完成,等待主应用程序完成并再次启动它(通过使用PID).如果你使用sparkle框架,你可以重用现有的框架:

     + (void) relaunch {
         NSString *relaunch = [[NSBundle bundleForClass:[SUUpdater class]] pathForResource:@"relaunch" ofType:@""];
         NSString *path = [[NSBundle mainBundle] bundlePath];
         NSString *pid = FMTStr(@"%d", [[NSProcessInfo processInfo] processIdentifier]);
         [NSTask launchedTaskWithLaunchPath:relaunch arguments:@[path, pid]];
         [NSApp terminate:self];
    }
Run Code Online (Sandbox Code Playgroud)

另一种选择是破解/Library/Application Support/com.apple.TCC/TCC.dbsqlite数据库使用辅助助手手动添加权限:

    NSString *sqlite = @"/usr/bin/sqlite3";
    NSString *sql = FMTStr(@"INSERT or REPLACE INTO access values ('kTCCServiceAccessibility', '%@', 1, 1, 1, NULL);", MY_BUNDLE_ID); 
    NSArray *args = @[@"/Library/Application Support/com.apple.TCC/TCC.db", sql];
    NSTask *task = [NSTask launchedTaskWithLaunchPath:sqlite arguments:args];
    [task waitUntilExit];
Run Code Online (Sandbox Code Playgroud)

然而,这将使应用程序无法成为应用商店.更多的是它真的只是一个黑客,db/schema可以随时更改.一些应用程序(例如Divvy.app用于执行此操作)在应用程序安装程序安装后脚本中使用此hack.这种方式可以防止对话框告知应用程序正在请求安装辅助工具.


Bre*_*nan 0

我在 GitHub 上找到了一个潜在的解决方案。

https://github.com/K8TIY/CW-Station

它有一个辅助应用程序,该应用程序将在根目录下运行以请求访问主应用程序。它有点过时了,并且正在使用一些已弃用的功能,因此我正在努力对其进行现代化改造。这看起来是一个很好的起点。