Xcode中如何通过代码运行命令行工具
2014-07-30 13:09
656 查看
// Run a command
+(NSString*)runCommand:(NSString*)cmd {
// Create an empty string
NSMutableString* mutableString = [NSMutableString
stringWithCapacity:0];
// Create a thread pool
NSAutoreleasePool *threadPool = [[NSAutoreleasePool
alloc]
init];
// Create a new task
NSTask *task = [[NSTask
alloc] init];
// Configure the task
[task setStandardOutput: [NSPipe
pipe]];
[task setStandardError: [task
standardOutput]];
[task
setLaunchPath:cmd];
// Launch the task
[task
launch];
// Append the output
NSData *data;
while ((data = [[[task
standardOutput] fileHandleForReading]
availableData]) && [data length]) {
NSString* output = [[[NSString
alloc] initWithData:data
encoding:NSASCIIStringEncoding]
autorelease];
[mutableString
appendString:output];
}
// Make sure the task is finished
[task
terminate];
// Release the thread pool
[threadPool
release];
// Return the output
return mutableString;
}
+(NSString*)runCommand:(NSString*)cmd {
// Create an empty string
NSMutableString* mutableString = [NSMutableString
stringWithCapacity:0];
// Create a thread pool
NSAutoreleasePool *threadPool = [[NSAutoreleasePool
alloc]
init];
// Create a new task
NSTask *task = [[NSTask
alloc] init];
// Configure the task
[task setStandardOutput: [NSPipe
pipe]];
[task setStandardError: [task
standardOutput]];
[task
setLaunchPath:cmd];
// Launch the task
[task
launch];
// Append the output
NSData *data;
while ((data = [[[task
standardOutput] fileHandleForReading]
availableData]) && [data length]) {
NSString* output = [[[NSString
alloc] initWithData:data
encoding:NSASCIIStringEncoding]
autorelease];
[mutableString
appendString:output];
}
// Make sure the task is finished
[task
terminate];
// Release the thread pool
[threadPool
release];
// Return the output
return mutableString;
}
相关文章推荐
- 如何运行ImageMagick的命令行工具
- 如何通过其他工具的命令行启动PowerShell
- Mac 通过命令行编译运行C代码 以及生成和调用静态库 以及Makefile实现过程
- 如何通过Git命令行把代码提交到github上
- 如何运行ImageMagick的命令行工具
- 在命令行模式下如何编译运行Java代码
- 如何通过Git命令行把代码提交到github上
- 如何通过Linux命令行使用和运行PHP脚本
- 如何通过命令行或者使用代码打开一个apk文件
- windows如何通过命令行查看memcache当前运行的状态
- 如何通过命令行或者使用代码打开一个apk文件
- 【小工具】通过命令行获得代码行数
- 当DataGridView控件通过代码在运行时设置了数据源,如何在运行时修改DataGridView控件的列
- mac os下通过命令行的方式编译c++代码并在xcode里引用
- Xcode/iOS: 如何判断代码运行在DEBUG还是RELEASE模式下?
- Xcode5 使用gitHub上库的SSH地址建立Repository,以及如何通过Xcode把代码传到GitHub
- 2017/12/23Java基础学习——如何通过记事本编写代码,并通过dos界面运行Java源文件
- 如何使用命令行编译和运行Java代码
- 如何通过键入命令运行“控制面板”工具
- 如何在代码中通过命令行创建SQL SERVER 数据库