在OS X下如何使用C++打开默认浏览器

8

我希望了解如何从C++应用程序打开OS X默认浏览器,然后打开所请求的URL。

编辑:我是这样解决的:

system("open http://www.apple.com");
2个回答

19

如果您喜欢使用原生的OS X API而不是system("open ...")

您可以使用以下代码:

#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

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接