2010-11-14 214 views
5

我想知道一種方法來從C++應用程序打開OS X上的默認瀏覽器,然後打開請求的URL。C++ OS X打開默認瀏覽器

編輯:我解決了它這樣的:

system("open http://www.apple.com"); 

回答

12

如果你喜歡使用的system("open ...")

本地OS X的API,而不是您可以使用此代碼:

#include <string> 
#include <CoreFoundation/CFBundle.h> 
#include <ApplicationServices/ApplicationServices.h> 

using namespace std; 

void openURL(const string &url_str) { 
    CFURLRef url = CFURLCreateWithBytes (
     NULL,      // allocator 
     (UInt8*)url_str.c_str(),  // URLBytes 
     url_str.length(),   // length 
     kCFStringEncodingASCII,  // encoding 
     NULL       // baseURL 
    ); 
    LSOpenCFURLRef(url,0); 
    CFRelease(url); 
} 

int main() { 
    string str("http://www.example.com"); 
    openURL(str); 
} 

,你必須用正確的OS X架構編譯:

g++ file.cpp -framework CoreFoundation -framework ApplicationServices 
相關問題