C++ OS X open default browser

笑着哭i 提交于 2019-12-18 22:23:12

问题


I would like to know a way to open the default browser on OS X from a C++ application and then open a requested URL.

EDIT: I solved it like this:

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

回答1:


In case you prefer using the native OS X APIs instead of system("open ...")

You can use this code:

#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);
}

Which you have to compile with the proper OS X frameworks:

g++ file.cpp -framework CoreFoundation -framework ApplicationServices



回答2:


Look at the docs for Launch Services.



来源:https://stackoverflow.com/questions/4177744/c-os-x-open-default-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!