加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在没有Apple开发人员证书的情况下,请求用户提升

发布时间:2020-12-16 07:53:02 所属栏目:百科 来源:网络整理
导读:显然,截至10.7,AuthorizationExecuteWithPrivileges已被弃用.我收集的信息的一般要点似乎建议使用ServiceManagement.framework的SMJobBless()函数来部署一个助手应用程序. 我的理解是,这需要一个从苹果公司购买的开发人员证书来代码签署我的应用程序和帮助程
显然,截至10.7,AuthorizationExecuteWithPrivileges已被弃用.我收集的信息的一般要点似乎建议使用ServiceManagement.framework的SMJobBless()函数来部署一个助手应用程序.

我的理解是,这需要一个从苹果公司购买的开发人员证书来代码签署我的应用程序和帮助程序,否则这将无法正常工作.它是否正确?

我最初使用AuthorizationExecuteWithPrivileges来请求用户提升权限,因为它们需要访问另一个正在运行的进程.没有这个,我的应用程序不能像非官方的插件一样工作.代码签名方式真的是从这里走的唯一途径?我试图避免购买开发商证书,因为它的成本很高.

有没有人找到任何替代方法来重新启动具有提升权限的应用程序,当然有用户许可?

解决方法

@CarlosP’s answer与代码逃脱路径&参数:
- (BOOL)runProcessAsAdministrator:(NSString*)scriptPath
                    withArguments:(NSArray*)arguments
                           output:(NSString**)output
                 errorDescription:(NSString**)errorDescription {

    //Check path.
    if (![scriptPath hasPrefix:@"/"]) {
        @throw [NSException exceptionWithName:
                    NSInvalidArgumentException reason:@"Absolute path required." userInfo:nil];
    }

    //Define script.
    static NSAppleScript* appleScript = nil;
    if (!appleScript) {
        appleScript = [[NSAppleScript alloc] initWithSource:
            @"on run commandWithArgumentsn"
             "  activaten"
             "  repeat with currentArgument in commandWithArgumentsn"
             "      set contents of currentArgument to quoted form of currentArgumentn"
             "  end repeatn"
             "  set AppleScript's text item delimiters to spacen"
             "  return do shell script (commandWithArguments as text) with administrator privilegesn"
             "end run"];
    }

    //Set command.
    NSAppleEventDescriptor* commandWithArguments = [NSAppleEventDescriptor listDescriptor];
    [commandWithArguments insertDescriptor:
        [NSAppleEventDescriptor descriptorWithString:scriptPath] atIndex:0];

    //Set arguments.
    for (NSString* currentArgument in arguments) {
        [commandWithArguments insertDescriptor:
            [NSAppleEventDescriptor descriptorWithString:currentArgument] atIndex:0];
    }

    //Create target & event.
    ProcessSerialNumber     processSerial   = {0,kCurrentProcess};
    NSAppleEventDescriptor* scriptTarget    =
        [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&processSerial length:sizeof(ProcessSerialNumber)];
    NSAppleEventDescriptor* scriptEvent     =
        [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass
                                                 eventID:kAEOpenApplication
                                        targetDescriptor:scriptTarget
                                                returnID:kAutoGenerateReturnID
                                           transactionID:kAnyTransactionID];
    [scriptEvent setParamDescriptor:commandWithArguments forKeyword:keyDirectObject];

    //Run script.
    NSDictionary*           errorInfo   = [NSDictionary dictionary];
    NSAppleEventDescriptor* eventResult = [appleScript executeAppleEvent:scriptEvent error:&errorInfo];

    //Success?
    if (!eventResult) {
        if (errorDescription)
            *errorDescription = [errorInfo objectForKey:NSAppleScriptErrorMessage];
        return NO;
    } else {
        if (output)
            *output = [eventResult stringValue];
        return YES;
    }

}

更新

在优胜美地,shell脚本只调用嵌入在StandardAdditions.osax中的AuthorizationExecuteWithPrivileges的version.

可以想象,当AuthorizationExecuteWithPrivileges做到时,shell脚本的管理员权限选项将消失.

就我个人而言,我会直接继续调用AuthorizationExecuteWithPrivileges.

做shell脚本自动具有reaping the process的优势.这需要一个小的extra work与AuthorizationExecuteWithPrivileges.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读