开发者

custom URL protocol for Cocoa Mac with Qt

开发者 https://www.devze.com 2023-03-20 05:45 出处:网络
I\'m trying to use a custom URL protocol to launch my Qt (4.7.3) application. I\'ve already got it working for Windows, so now it is time for the Mac.

I'm trying to use a custom URL protocol to launch my Qt (4.7.3) application. I've already got it working for Windows, so now it is time for the Mac.

I've already done the necessary changes to the Info.plist, and have the application being launched when one enters 'customrprot开发者_如何学编程ocol://etc' in a browser. The problem is that I need some way of passing that entire URL into my application so I can process the arguments (only at the beginning of the application). At this stage no extra argument (argv[1]) is found like in Windows, despite typing out long URLs with my custom URL protocol.

At the moment, I've investigated two potential ways to do this:

1) Make an apple script that I turn into an application bundle whose .plist I modify to respond to customprotocol://, and have that simply launch my real application with 'do shell script' and pass in the URL as an argument. http://www.macosxautomation.com/applescript/linktrigger/index.html explains this process.

Not sure if I like this option, since you essentially have 2 application bundles at that point and I don't want to have to put two different applications on someone's Mac for this to work. I'm experimenting with putting one bundle in another to see if it works.

2) Handle the actual 'geturl' AppleEvent by incorporating objective-c into my project, which I've tried to do by following this example: URL scheme - Qt and mac.

My problem with this option is that I'm not quite sure how to go about it; I've thrown all the objective-c code into a .mm file that I bring into the project with an OBJECTIVE_SOURCES += objectivecstuff.mm, and I make sure to include the Cocoa framework as a library as well.

Everything compiles fine, but it simply doesn't do what I want it to do (I additionally try to throw the URL into a global variable in the linked example's 'handleGetURLEvent' method so I can process it in a different module).

I think the crux of the problem is that I don't understand the objective-c in that .mm file and am not sure if I need to somewhere instantiate this EventHandler object or whatever. I've tried reading through http://el-tramo.be/blog/mixing-cocoa-and-qt but this guy's example of integrating Cocoa into Qt looks far more C++-oriented then what appears to be the necessary stuff found in URL scheme - Qt and mac, so I'm not sure how to proceed with this option.

Would really appreciate some advice and suggestions at this point!

Thank you for reading.


In order to process URLs through Apple Events with Cocoa, you need a few things:

  • You need to register your protocol in Info.plist using CFBundleURLTypes. This is how you tell Launch Services that your application is capable of handling a given scheme. Launch Services looks through all the applications on the system, reading their Info.plist files, and creates a big database of them. You can see how you look to LS by running

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump
    

    (Sorry its path is so long; it just is.) You can use lsregister to re-register your app if you need to, but it'll generally do it automatically.

  • Since you're using Qt, I don't believe you have an NSApplication, so there's not a hook to run the code you're providing. That's ok. You really just wanted to respond to the Apple Event, not run Cocoa. Nokia includes docs on that. Hopefully QFileOpenEvent will meet your need without having to use Carbon, but if you do, this part of Carbon is fine (it's not deprecated like other parts of Carbon are). The upside of using Carbon or QFileOpenEvent is that you can do all your work in C.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号