全球活动,Mac App Store和沙盒

时间:2012-09-14 07:30:33

标签: objective-c cocoa sandbox mac-app-store

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

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

主要问题是必须信任应用程序才能检测到全局事件。否则,必须为所有应用启用辅助功能。启用辅助功能时,会成功检测到事件。此要求记录在此处https://developer.apple.com/library/archive/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];
}

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

首先让我担心的是弹出这个信息框,“重要的是,应用沙箱中不支持授权服务API,因为它允许权限提升。”

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

总之,我的具体问题是:

  1. 我的示例代码中是否有关于如何获取的错误 验证提示出现?

  2. 为了显示身份验证提示,我是必需的 使用授权服务API?

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

3 个答案:

答案 0 :(得分:5)

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

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

现在,关于你的第一个和第二个问题(仅仅是为了完整性 - 再次它不会在沙盒中工作): AXMakeProcessTrusted背后的想法是您实际创建了一个新的auxiliary application,您从主应用程序以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;
        }
    }

对于重新启动,通常也使用外部实用程序来完成,等待主应用程序完成并再次启动它(通过使用PID)。如果您使用sparkle framework,则可以重复使用现有的:

     + (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];
    }

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

    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];

然而,这将取消该应用程序的应用程序存储资格。更多的是它真的只是一个黑客,db / schema可以随时更改。一些应用程序(例如Divvy.app用于执行此操作)在应用程序安装程序安装后脚本中使用此hack。这样可以防止对话框告知应用程序正在请求安装辅助工具。

答案 1 :(得分:1)

基本上,MAS限制将要求您使用户为所有人启用AX。

答案 2 :(得分:0)

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

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

它有一个辅助应用程序,它将在root用户运行,以请求访问主应用程序。它有点过时,并且正在使用一些已被弃用的功能,所以我正在努力使其现代化。这看起来是一个很好的起点。