C ++ OS X打开默认浏览器 [英] C++ OS X open default browser

查看:94
本文介绍了C ++ OS X打开默认浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我这样解决了它:

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

推荐答案

如果您更喜欢使用本机OS X API而不是system("open ...")

In case you prefer using the native OS X APIs instead of 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框架进行编译:

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

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

这篇关于C ++ OS X打开默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆