【发布时间】:2014-08-04 15:32:13
【问题描述】:
据我所知,这段代码应该以提升的权限运行我的脚本,但 NSTask 似乎并没有真正启动。我对 Objective-C 和 Cocoa 比较陌生,所以我可能在这里遗漏了一些简单的东西,但无论哪种方式,我都不知道为什么这不起作用。
CFErrorRef error;
AuthorizationItem authItem = { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
AuthorizationRights authRights = { 1, &authItem };
AuthorizationFlags flags = kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
AuthorizationRef auth;
if(AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &auth) == errAuthorizationSuccess)
{
NSString* myLabel = @"com.hidden.backup";
(void) SMJobRemove( kSMDomainSystemLaunchd, (__bridge CFStringRef)myLabel, auth, false, NULL );
NSString *path = [[NSBundle mainBundle] pathForResource:@"kandorBackup" ofType:@"sh"];
NSPipe *outputPipe = [NSPipe pipe];
readHandle = [outputPipe fileHandleForReading];
inData = nil;
returnValue = nil;
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:path];
[task setArguments:[NSArray arrayWithObjects:login, selectedDrive, rsyncFinalFlags, nil]];
[task setStandardOutput:outputPipe];
[readHandle waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(recievedData:)
name:NSFileHandleDataAvailableNotification
object:nil];
SMJobSubmit( kSMDomainSystemLaunchd, (__bridge CFDictionaryRef)task, auth, &error);
}
如果有帮助,我会在 Xcode 调试区域收到这两个错误:
2014-08-04 11:44:39.169 <hidden>[68065:303] -[NSConcreteTask objectForKey:]: unrecognized selector sent to instance 0x600000099d70
2014-08-04 11:44:39.169 <hidden>[68065:303] Exception detected while handling key input.
【问题讨论】: