Using NSTask in iOS Console Application

nstask

在Mac OS下要在命令行中调用另外一个二进制程序可以使用NSTask但是在iOS上如果要使用这个东西就会发现一个问题:找不到NSTask定义,其实问题的关键是找不到相关的头文件。解决办法也很简单,直接把下面的NSTask头文件放到项目的解决方案下再调用就行了。

测试代码:

//
//  main.cpp
//  NSTask
//
//  Created by obaby on 14-2-27.
//  Copyright (c) 2014年 __MyCompanyName__. All rights reserved.
//

#include 
#include "NSTask.h"
#include 

void PingTest();

void PingTest()
{
    NSTask *task;
    task = [[NSTask alloc ]init];
    [task setLaunchPath:@"/usr/bin/ping"];
        
    NSLog(@"This is NSTask with ping command......\n");
    NSArray *arguments;
    arguments = [NSArray arrayWithObjects:@"www.h4ck.org.cn", 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(@"%@",string);
}

int main (int argc, const char * argv[])
{

    // insert code here...
    PingTest();
    //std::cout < < "Hello, World!\n";
	return 0;
}

头文件代码(NSTask.h):

//
//  NSTask.h
//  ipacracker
//
//  Created by obaby on 14-2-10.
//
//

#ifndef ipacracker_NSTask_h
#define ipacracker_NSTask_h

/*	NSTask.h
 Copyright (c) 1996-2007, Apple Inc. All rights reserved.
 */

#import 

@class NSString, NSArray, NSDictionary;

@interface NSTask : NSObject

// Create an NSTask which can be run at a later time
// An NSTask can only be run once. Subsequent attempts to
// run an NSTask will raise.
// Upon task death a notification will be sent
//   { Name = NSTaskDidTerminateNotification; object = task; }
//

- (id)init;

// set parameters
// these methods can only be done before a launch
- (void)setLaunchPath:(NSString *)path;
- (void)setArguments:(NSArray *)arguments;
- (void)setEnvironment:(NSDictionary *)dict;
// if not set, use current
- (void)setCurrentDirectoryPath:(NSString *)path;
// if not set, use current

// set standard I/O channels; may be either an NSFileHandle or an NSPipe
- (void)setStandardInput:(id)input;
- (void)setStandardOutput:(id)output;
- (void)setStandardError:(id)error;

// get parameters
- (NSString *)launchPath;
- (NSArray *)arguments;
- (NSDictionary *)environment;
- (NSString *)currentDirectoryPath;

// get standard I/O channels; could be either an NSFileHandle or an NSPipe
- (id)standardInput;
- (id)standardOutput;
- (id)standardError;

// actions
- (void)launch;

- (void)interrupt; // Not always possible. Sends SIGINT.
- (void)terminate; // Not always possible. Sends SIGTERM.

- (BOOL)suspend;
- (BOOL)resume;

// status
- (int)processIdentifier;
- (BOOL)isRunning;

- (int)terminationStatus;

@end

@interface NSTask (NSTaskConveniences)

+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments;
// convenience; create and launch

- (void)waitUntilExit;
// poll the runLoop in defaultMode until task completes

@end

FOUNDATION_EXPORT NSString * const NSTaskDidTerminateNotification;


#endif


文件链接:http://pan.baidu.com/share/link?shareid=3657138684&uk=3188888025

☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《Using NSTask in iOS Console Application》
* 本文链接:https://oba.by/2014/02/5443
* 短链接:https://oba.by/?p=5443
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注