Nw.js: Implement URL scheme support for MAC in 0.13

Created on 14 Jan 2016  路  17Comments  路  Source: nwjs/nw.js

Please implement this in 0.13 - we are depending on this functionality, so that our app can be opened with custom url scheme - it was already on 0.12!

The code is just is just the 98b1ef8 + achwedyk@6f3ba7c

This fix is only needed for the mac - on Windows it is all ok!

On 0.12 it was implemented with https://github.com/nwjs/nw.js/pull/1769

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

nw13

Most helpful comment

@snehapshinde - all works fine from the example above, you just need to handle the open event, like this in the latest nwjs:

nw.App.on('open', function(url) {
    console.log('APP OPEN EVENT! cmd: ' + url);
        // do something with the passed url
});

This will handle the event when user triggers the user protocol and your app is already open.

If you want to handle the case when your app is not yet open, make sure you also check the command line arguments _nw.App.argv_ for the same sort url as parameter when your app is fully initialized.

All 17 comments

Testing this it very easy on any nwjs app, just follow the following few steps.
Here I will register the protocol test333://

  • if you show the Contents of the app, under Contents, edit Info.plist
  • in the beginning just after the dict tag add:
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>Test Extension</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>test333</string>
            </array>
        </dict>
    </array>    
  • in your index,html or main file under app.nw, add something like:
var Gui = require('nw.gui');
Gui.App.on('open', function(url) {
    console.log('APP OPEN EVENT! cmd: ', url);
});
  • open you app once and close it (protocol gets registered from the Info.plist)
  • open safari and enter a url with the registered protocol like this:
    test333://somecustom_string
  • if everything is OK you app should be opened and the open event should be triggered with the url parameter to it
  • if you app was already open it should be activated and only the open event fired again with the passed url

On Windows the registration process is different but it results on the same of the app being started/or activated if running and the open event fired with the url.
You can see the Windows registration process here: http://stackoverflow.com/questions/389204/how-do-i-create-my-own-url-protocol-e-g-so

Fixed in git and will be available in the next nightly build.

@rogerwang crash is fixed on beta6, but open event argument is undefined. Following @gpetrov example:

APP OPEN EVENT! cmd:  undefined

Am I missing something?

@rogerwang I also don't see where this new function OSXOpenURLsHook is called ...
Very excited about getting this to work!

@k7k0 Did you got the app open event on each invocation of the custom url? Even when the app is already active? So not only when started initially?

@gpetrov I'm receiving the open event only when the app is already active. On initial launch no event is called, I'm looking for nw.App.argv arguments (that works on windows) but on OSX is not coming either.

sorry, actually beta6 doesn't contain the full fix. will fix it again asap.

@gpetrov the hook is called from here: https://github.com/nwjs/chromium.src/blob/nw13/chrome/browser/app_controller_mac.mm#L1384

Fixed again in git and will be available in the next nightly build.

@rogerwang great, now is working alright!

  • On launch url is available using nw.App.argv
  • While running the event nw.App.on('open') receives the url

Thanks!

I am not getting the url on 'open' event.

This is my sample start.html:
start.txt

and package.json:
package.txt

info.plist:
Info.txt

The application gets registered with test333, but alert message never shows the url.

I tried with 0.13.0 as well as latest nwjs version.
Am I missing something?

@gpetrov Please guide me on how to get the URL. Does it take anything extra to do other than what is mentioned in your test333 example?

@snehapshinde - all works fine from the example above, you just need to handle the open event, like this in the latest nwjs:

nw.App.on('open', function(url) {
    console.log('APP OPEN EVENT! cmd: ' + url);
        // do something with the passed url
});

This will handle the event when user triggers the user protocol and your app is already open.

If you want to handle the case when your app is not yet open, make sure you also check the command line arguments _nw.App.argv_ for the same sort url as parameter when your app is fully initialized.

Ok, thanks. I think I was doing something wrong. It is working fine!

I have couple of questions:

  1. can we also register protocols like 'mailto' with this approach. I tried doing it, but it was not registering for mailto, but worked for any other custom protocol. Maybe because mailto protocol is already registered with other app?
  2. Once the app is registered with that protocol, is there any provision to unregister it from nwjs?

Thanks for your reply.

@gpetrov Kindly reply to my queries. Waiting for it.

@snehapshinde this is not a support forum, please check the apple docs on implementing custom protocols

Ok. Thanks. I will check.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sorty-zz picture sorty-zz  路  4Comments

xzh-loop picture xzh-loop  路  3Comments

jportoles picture jportoles  路  3Comments

niutech picture niutech  路  4Comments

imyzf picture imyzf  路  4Comments