C ++ OSX开放默认的浏览器! [英] c++ OSX open default browser!

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

问题描述

我想知道的方式++来打开从C OSX的默认浏览器应用程序,然后打开一个请求的URL。

谢谢!

编辑:我解决它是这样的:

 系统(打开http://www.apple.com);


解决方案

如果你使用$本地OS X的API p $ PFER代替系统(开...)

您可以使用此code:

 的#include<串GT;
#包括LT&;的CoreFoundation / CFBundle.h>
#包括LT&; ApplicationServices / ApplicationServices.h>使用命名空间std;无效的OpenURL(常量字符串和放大器; URL_STR){
  CFURLRef URL = CFURLCreateWithBytes(
      NULL,//分配器
      (UINT8 *)url_str.c_str(),// URLBytes
      url_str.length(),//长度
      kCFStringEncodingASCII,//编码
      NULL // BASEURL
    );
  LSOpenCFURLRef(URL,0);
  CFRelease(URL);
}诠释主(){
  字符串str(http://www.example.com);
  的OpenURL(STR);
}

,你必须用正确的OS X架构编译:

  G ++ file.cpp -framework的CoreFoundation -framework ApplicationServices

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

thanks!

EDIT: I solved it like this:

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

解决方案

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

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

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