C++ OS X open default browser

前端 未结 2 1157
北荒
北荒 2021-01-05 04:46

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:

sys         


        
相关标签:
2条回答
  • 2021-01-05 05:25

    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
    
    0 讨论(0)
  • 2021-01-05 05:27

    Look at the docs for Launch Services.

    0 讨论(0)
提交回复
热议问题