Phi*_*ill 34 macos xcode objective-c
自从更新到OSX 10.7 Lion以来,Xcode告诉我AuthorizationExecuteWithPrivileges已弃用.
任何人都可以建议我的应用程序可以写入一个它没有权限的目录吗?
小智 34
实际上,AuthorizationExecuteWithPrivileges()已经被弃用很长一段时间了,直到最近头文件才赶上了这个事实.
您可以创建特权帮助工具作为应用程序的一部分.您可以使用ServiceManagement.framework的SMJobBless()功能有部署到系统中的辅助launchd背景:那么当你需要执行特权任务,你只要消息特权帮助做工作.
有一点隐藏的复杂性,因为应用程序和帮助程序必须先声明另一个的签名标识,然后才SMJobBless()认为它们应该一起使用,并且需要让链接器将帮助程序工具的Info.plist文件写入二进制文件.这一切都被覆盖苹果的文档和苹果已经提供了一个示例项目,太.
我写了一个示例应用程序使用SMJobBless()来部署其优越的帮手.
小智 34
我知道这听起来很疯狂,但这确实有效:
NSDictionary *error = [NSDictionary new];
NSString *script = @"do shell script \"whoami > /tmp/me\" with administrator privileges";
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
if ([appleScript executeAndReturnError:&error]) {
NSLog(@"success!");
} else {
NSLog(@"failure!");
}
Run Code Online (Sandbox Code Playgroud)
我正在从Objective C执行一个Applescript.唯一的缺点是你无法获得永久的root权限.每次运行时都会询问密码.
Car*_*s P 17
根据user950473的一个很好的发现,我已经将他/她的发现作为一种方法实现了; 以为我会分享这些代码以防它有用.
- (BOOL) runProcessAsAdministrator:(NSString*)scriptPath
withArguments:(NSArray *)arguments
output:(NSString **)output
errorDescription:(NSString **)errorDescription {
NSString * allArgs = [arguments componentsJoinedByString:@" "];
NSString * fullScript = [NSString stringWithFormat:@"'%@' %@", scriptPath, allArgs];
NSDictionary *errorInfo = [NSDictionary new];
NSString *script = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];
// Check errorInfo
if (! eventResult)
{
// Describe common errors
*errorDescription = nil;
if ([errorInfo valueForKey:NSAppleScriptErrorNumber])
{
NSNumber * errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber];
if ([errorNumber intValue] == -128)
*errorDescription = @"The administrator password is required to do this.";
}
// Set error message from provided message
if (*errorDescription == nil)
{
if ([errorInfo valueForKey:NSAppleScriptErrorMessage])
*errorDescription = (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage];
}
return NO;
}
else
{
// Set output to the AppleScript's output
*output = [eventResult stringValue];
return YES;
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
NSString * output = nil;
NSString * processErrorDescription = nil;
BOOL success = [self runProcessAsAdministrator:@"/usr/bin/id"
withArguments:[NSArray arrayWithObjects:@"-un", nil]
output:&output
errorDescription:&processErrorDescription];
if (!success) // Process failed to run
{
// ...look at errorDescription
}
else
{
// ...process output
}
Run Code Online (Sandbox Code Playgroud)
它有点hacky,但恕我直言是一个令人满意的解决方案.
AuthorizationExecuteWithPrivileges确实已弃用.
但幸运的是,有一种新的推荐方法可以继续.
从10.6开始,有新的API,建议安装一个将执行特权操作的帮助工具.Apple提供了一个代码示例,清楚地演示了如何管理它.
确保你看看他们的readme.txt,因为与其他代码示例相反,除了下载项目并运行它之外还有更多工作要做.
来自SMJobBless示例介绍
SMJobBless演示了如何安全地安装执行特权操作的帮助工具以及如何将该工具与调用它的应用程序相关联.
从Snow Leopard开始,这是在Mac OS X上管理权限提升的首选方法,应该使用它来代替早期方法,例如BetterAuthorizationSample或直接调用 AuthorizationExecuteWithPrivileges.
SMJobBless使用Mac OS X v10.6 Snow Leopard中引入的ServiceManagement.framework.
| 归档时间: |
|
| 查看次数: |
19265 次 |
| 最近记录: |