由於共享關愛這裏是執行腳本的方法:我更新的代碼,以便更爲通用:
-(void) runScript:(NSString*)scriptName withVariables:(NSDictionary *)variables
{
NSString *appSupportPath = [NSFileManager defaultManager] applicationSupportDirectory];
NSArray *arguments;
NSString* newpath = [NSString stringWithFormat:@"%@/%@",appSupportPath, scriptName];
if ([[NSFileManager defaultManager]fileExistsAtPath:newpath]){
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: newpath];
NSLog(@"Executing hook: %@",newpath);
arguments = [NSArray arrayWithObjects:newpath, nil];
[task setArguments: arguments];
[task setEnvironment:variables];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"script returned:\n%@", string);
}
}
}
更新。現在NSTask會告訴內核直接執行這個腳本,這樣你的用戶就不能在線使用Bash腳本,而且還可以使用python,perl,php。她需要使用的唯一東西是該文件中的Shebang。
可以找到NSFileManager類別here。
好的問題很簡單:http://www.cocoawithlove.com/2010/05/finding-or-creating-application-support.html – plaetzchen