如果權限都OK,那麼它聽起來像你可能不會得到正確的shell環境。對於像mkdir這樣的許多簡單命令,Cocoa等價物是可以使用的,但是如果你更喜歡命令行,我會建議使用NSTask對象模型,這對我來說非常好。這是我自己適應的一種相當常見的模式,供您嘗試。
-(NSString *) runThisCmd:(NSString *) runString withArgs:(NSArray *)runArgs {
//this method of calling NSTask is designed for small quick commands that return
//a small amount of data to the command line
NSTask *task = [NSTask new];
[task setLaunchPath:runString];
[task setArguments:runArgs];
[task setStandardOutput:[NSPipe pipe]];
[task setStandardInput: [NSPipe pipe]];
[task setStandardError: [task standardOutput]];
[task launch];
NSData *stdOuput = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
if ([task terminationStatus] == 0) {
//scriptSuccess = YES;
}
else {
//scriptSuccess = NO;
}
NSString *outputString = [[NSString alloc] initWithData:stdOuput encoding:NSUTF8StringEncoding];
return outputString;
}
調用它像這樣:
NSString *runCmd = [[NSString alloc] initWithString:@"/bin/bash"];
NSArray *runArgs = [[NSArray alloc] initWithObjects:@"-c",@"mkdir",@"/tmp/newdir",nil];
//update proper label
NSString *output;
output = [self runThisCmd:runCmd withArgs:runArgs];
注意:你能適應這種方法如果使用[任務結束]邏輯和下降返回字符串行爲就像系統()命令。即使您不需要它,輸出也可以幫助您排除錯誤。
感謝您的建議,我會努力讓您知道。 – shatthi 2012-03-02 06:49:44