C++ OS X打开默认浏览器

mok*_*oka 8 c++ browser url macos

我想知道一种从C++应用程序打开OS X上的默认浏览器然后打开请求的URL的方法.

编辑:我这样解决了:

system("open http://www.apple.com");
Run Code Online (Sandbox Code Playgroud)

Ale*_*min 15

如果您更喜欢使用本机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);
}
Run Code Online (Sandbox Code Playgroud)

您必须使用适当的OS X框架进行编译:

g++ file.cpp -framework CoreFoundation -framework ApplicationServices
Run Code Online (Sandbox Code Playgroud)