gpt4 book ai didi

cocoa - 让 NSTask 和 SMJobSubmit 正常运行的问题

转载 作者:行者123 更新时间:2023-12-03 17:51:35 32 4
gpt4 key购买 nike

据我所知,这段代码应该以提升的权限运行我的脚本,但 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.

最佳答案

SMJobSubmit 不适用于提升 NSTask。您宁愿以与 LaunchDaemon/LaunchAgent plist 文件相同的格式构建 NSDictionary,并将该 NSDictionary 传递给 SMJobSubmit。

你会这样做

NSDictionary *dictionary = @{@"ProgramArguments": @[path,login, selectedDrive, rsyncFinalFlags]};
SMJobSubmit( kSMDomainSystemLaunchd, (__bridge CFDictionaryRef)dictionary, auth, &error);

不幸的是,您不会收到任何进度消息,如果这是必要的,您需要构建一个特权帮助工具并从那里执行 NSTask。

https://developer.apple.com/library/mac/samplecode/EvenBetterAuthorizationSample/Listings/Read_Me_About_EvenBetterAuthorizationSample_txt.html

关于cocoa - 让 NSTask 和 SMJobSubmit 正常运行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122332/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com