electron-deep-linking-mac-win icon indicating copy to clipboard operation
electron-deep-linking-mac-win copied to clipboard

Not working anymore on Mac on start

Open sergeyd63 opened this issue 4 years ago • 4 comments

With electron 9, on mac, when the app is starting the link is not passed to the app only when the app is already open the "open-url" event captures the protocol

wonder if you have a solution thanks

sergeyd63 avatar May 29 '20 21:05 sergeyd63

Is it working if you are moving the open-url event out of the will-finish-launching event?

I read on stackoverflow that someone else had a similar issue but to run open-url in the will-finish-launching is still the recommended way in the electron docs. To move it out of this event may cause other problem but it's worth a try I guess.

sapkra avatar Jun 20 '20 03:06 sapkra

There is one solution (this one) all over the web. It doesn't work anymore. I guess it used to...

sergeyd63 avatar Jun 20 '20 03:06 sergeyd63

Me too. Seek solutions

zhihuifanqiechaodan avatar Jun 28 '20 02:06 zhihuifanqiechaodan

Binding open-url before ready seems to work as of electron@^10.1.3. You need to handle the race cond. between open-url and ready in that case. I ended up doing sth. like this:

// set when the app receives a`ready` event for the first time, indicating that setup logic has run
let isAppLaunched = false;
// on macOS, `open-url` is received before `ready` when opened via a protocol url
// we need to keep a reference of the url around in that case to later apply it in `ready`
let protocolOpenerUrl;

function createWindow(protocolUrl) {}
function restoreOrCreateWindow(protocolUrl) {}

  app.on('open-url', (evt, url) => {
    evt.preventDefault();
    protocolOpenerUrl = url;
    // when not launched yet, we store a reference to the url which is picked up in `ready`
    // when launched, we create or restore a window
    if (isAppLaunched) restoreOrCreateWindow(url);
  });

  app.on('second-instance', (_, argv) => {
    if (IS_LINUX || IS_WIN) restoreOrCreateWindow(argv[argv.length - 1]);
  });

  app.on('ready', async () => {
    isAppLaunched = true;
    // on macOS, use stored url reference
    // on linux and win use process.argv[1]
    createWindow(IS_MAC ? protocolOpenerUrl : process.argv[process.argv.length - 1]);
  });

fspoettel avatar Nov 06 '20 13:11 fspoettel