cocoa调用mysql_从Cocoa应用程序执行终端命令

b77e94a153ecf0910218344b3c4dbd58.png

波斯汪

本着分享的精神.。这是我经常使用的运行shell脚本的方法。您可以将脚本添加到产品包中(在构建的复制阶段),然后在运行时读取并运行脚本。注意:此代码在PrivateFrameworks子路径中查找脚本。警告:对于已部署的产品来说,这可能是一个安全风险,但是对于我们的内部开发来说,定制简单的东西是一种简单的方法(比如rsync到.)而不需要重新编译应用程序,而只是编辑包中的shell脚本。//-------------------------------------------------------(void) runScript:(NSString*)scriptName{

    NSTask *task;

    task = [[NSTask alloc] init];

    [task setLaunchPath: @"/bin/sh"];

    NSArray *arguments;

    NSString* newpath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] privateFrameworksPath], scriptName];

    NSLog(@"shell script path: %@",newpath);

    arguments = [NSArray arrayWithObjects:newpath, nil];

    [task setArguments: arguments];

    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);    }//------------------------------------------------------编辑:包括NSLog问题的修复如果您正在使用nstask通过bash运行命令行实用程序,那么需要包含这条神奇的行以保持NSLog正常工作://The magic line that keeps your log where it belongs[task setStandardInput:[NSPipe pipe]];在以下方面:NSPipe *pipe;pipe = [NSPipe pipe];[task setStandardOutput: pipe];//The magic line that keeps your log where it belongs[task setStandardInput:[NSPipe pipe]];这里有一个解释:http://www.cocoadev.com/index.pl?NSTask

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值