我有这段代码可以检查某些设置并记录结果。第一次设置检查工作正常,但下一次失败。这是代码块:
//init classes
CocoaLogging *logFile = [[CocoaLogging alloc]init];
NSString * result;
NSInteger * state;
NSPipe *pipe=[[NSPipe alloc] init];
NSFileHandle *handle;
NSString * cmd = [NSString stringWithFormat:@"%@", @"/usr/bin/defaults"];
//Start logging
NSString *logText;
NSString *logPath;
BOOL logSuccess;
NSLog(@"Start Settings Enforcer");
logPath = [NSString stringWithFormat:@"%@%@", NSHomeDirectory(), LOGFILE_PATH];
if (! [[NSFileManager defaultManager] fileExistsAtPath:logPath isDirectory:NO])
{
logSuccess = [logFile createLogFile:logPath];
if (logSuccess)
{
logText = [NSString stringWithFormat:@"%@", @"Start Settings Enforcer"];
[logFile makeLogEntry:logText to:logPath];
}
}
else
{
logPath = [NSString stringWithFormat:@"%@%@", NSHomeDirectory(), LOGFILE_PATH];
logText = [NSString stringWithFormat:@"%@", @"Start Settings Enforcer"];
logSuccess = [logFile makeLogEntry:logText to:logPath];
}
//check status of firewall
//defaults read "/Library/Preferences/com.apple.alf" globalstate
//array of commandline args
NSMutableArray *firewallArgs = [[NSMutableArray alloc]initWithObjects:@"-currentHost", @"read", @"/Library/Preferences/com.apple.alf", @"globalstate", nil];
//init the task
NSTask *firewall=[[NSTask alloc] init];
//define the command to run
[firewall setLaunchPath:cmd];
[firewall setArguments:firewallArgs];
[firewall setStandardOutput:pipe];
handle=[pipe fileHandleForReading];
//run the command
[firewall launch];
// convert NSData -> NSString
result = [[NSString alloc] initWithData:[handle readDataToEndOfFile]encoding:NSASCIIStringEncoding];
state = [result intValue];
NSLog(@"%@", result);
if (state == 1)
{
NSLog(@"firewall is on");
logText = [NSString stringWithFormat:@"%@", @"firewall is on"];
logSuccess = [logFile makeLogEntry:logText to:logPath];
}
else
{
NSLog(@"firewall is off");
logText = [NSString stringWithFormat:@"%@", @"firewall is off"];
logSuccess = [logFile makeLogEntry:logText to:logPath];
}
[firewallArgs removeAllObjects];
[handle closeFile];
[firewall terminate];
//check screensaver on
//defaults read ~/Library/Preferences/com.apple.screensaver askForPassword
NSString * plistPath = [NSString stringWithFormat:@"%@", @"~/Library/Preferences/com.apple.screensaver"];
plistPath = [plistPath stringByExpandingTildeInPath];
//array of commandline args
NSMutableArray *ssOnArgs = [[NSMutableArray alloc]initWithObjects:@"read", plistPath, @"askForPassword", nil];
//init the task
NSTask *ssOn=[[NSTask alloc] init];
//define the command to run
[ssOn setLaunchPath:cmd];
[ssOn setArguments:ssOnArgs];
[ssOn setStandardOutput:pipe];
handle=[pipe fileHandleForReading];
//run the command
[ssOn launch];
// convert NSData -> NSString
result = [[NSString alloc] initWithData:[handle readDataToEndOfFile]encoding:NSASCIIStringEncoding];
state = [result intValue];
if (state == 1)
{
NSLog(@"screensaver is on");
logText = [NSString stringWithFormat:@"%@", @"screensaver is on"];
logSuccess = [logFile makeLogEntry:logText to:logPath];
}
else
{
NSLog(@"screensaver is off");
logText = [NSString stringWithFormat:@"%@", @"screensaver is off"];
logSuccess = [logFile makeLogEntry:logText to:logPath];
}
NSLog(@"Check-in complete");
logText = [NSString stringWithFormat:@"%@", @"Check-in complete."];
logSuccess = [logFile makeLogEntry:logText to:logPath];
}
return 0;
这里是错误:
2014-03-31 12:58:11.630 SettingsEnforcer[5379:303] * Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: ' -[NSConcreteFileHandle fileDescriptor]: No such process' ** First throw call stack: ( 0 CoreFoundation 0x00007fff8ce4825c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x00007fff8b075e75 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8ce4810c +[NSException raise:format:] + 204 3 Foundation 0x00007fff8ba9a29d -[NSConcreteFileHandle fileDescriptor] + 30 4 Foundation 0x00007fff8bb2fb97 -[NSConcreteTask launchWithDictionary:] + 2114 5 SettingsEnforcer 0x0000000100001e1e main + 2126 6 libdyld.dylib 0x00007fff91e555fd start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
我希望比我聪明的人能发现错误。
最佳答案
您正在回收 NSPipe
实例。为第二个任务创建一个新管道。
关于objective-c - NSException 重复 NSTask 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22771291/