MAC COCOA call command 調用終端控制台程式

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   http   

MAC COCOA call command 調用終端控制台程式

STEP 1 

先寫一個C++ DOS程式

STEP2

使用NSTask來執行,然後用NSPipeNSData來接受執行的結果字串。

先寫一個函數:

/******************************* Function : NSTask Class + Terminal(=Windows Command) of MAC OS X Description : $ ./test_was_call_command_app -sum 2 8 5 *******************************/NSString* test_sum(int a,int b,int c){    NSString *path  =  @"/Users/test_was_call_command_app/debug/test_was_call_command_app";    NSTask *task            = [[NSTask alloc] init];    task.launchPath = path;    NSMutableArray *arguments = [[NSMutableArray alloc] init];    [arguments addObject:@"-sum"];    NSString *var;    var =[ NSString stringWithFormat:@"%i",a ];    [arguments addObject:var];    var =[ NSString stringWithFormat:@"%i",b];    [arguments addObject:var];    var =[ NSString stringWithFormat:@"%i",c ];    [arguments addObject:var];    task.arguments  = arguments;//啟動前,增加輸出設定+++    NSPipe *pipe;    pipe = [NSPipe pipe];    [task setStandardOutput: pipe];    NSFileHandle *file;    file = [pipe fileHandleForReading];// ---        [task launch];    [task waitUntilExit];    //執行結束後,得到執行的結果字串++++++    NSData *data;    data = [file readDataToEndOfFile];    NSString *result_str;    result_str = [[NSString alloc] initWithData: data                                   encoding: NSUTF8StringEncoding];    //---------------------------------    return result_str;}
然後在MAIN函數裡面調用

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

{

    @autoreleasepool

    {

       NSString *rs=test_sum(7,2,20);

       int rs_int= [rs intValue];

       NSString *show =[NSStringstringWithFormat:@"sum : %i",rs_int ];

        

       CFShow(show);

  }



STEP3

測試,成功!

補充:

   // [以下兩行可以在MAC APP中點擊按鈕開啟一個網頁] +++

    NSURL *web_url = [NSURLURLWithString:@"http://www.hao123.com/"];

    [[NSWorkspacesharedWorkspace] openURL:web_url];//TEST OK!

    // [以下兩行可以在MAC APP中點擊按鈕開啟一個網頁] ---


   // Launch Apple Mail [test OK!]

   [[NSWorkspacesharedWorkspace]openURL:[NSURLURLWithString:@"mailto://[email protected]"]];



  或者:

- (IBAction)OnClick_BT_OpenURLBySearch:(id)sender

{

    //開啟網頁的第二種方法,傳遞一個var [begin] +++

   NSString* searchQuery = @"apple";

    

    // Be careful to always URL encode things like spaces and other symbols that aren‘t URL friendly

    searchQuery =  [searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

    // Now create the URL string ...

   NSString* urlString = [NSStringstringWithFormat:@"http://www.baidu.com/s?wd=%@", searchQuery];

    

    // An the final magic ... openURL!

    [[NSWorkspacesharedWorkspace] openURL:[NSURLURLWithString:urlString]];

    //開啟網頁的第二種方法,傳遞一個var [begin] ---

}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.