The background,

Apps that want to be aroused by other apps should be a very common need

How to arouse

It’s been a long search, all based on protocol. protocol

Then you find the key code

The website address

app.setAsDefaultProtocolClient(PROTOCOL, process.execPath, args);
Copy the code

But here’s the key: you have to run the program at least once to register this protocol!!

It’s hard.

So we need to think, is there a way to put the protocol in at the time of installation?

The answer is: Yes.

How to write the protocol at installation time?

Since you’re going to write it at installation time, you definitely need to work on the packing.

Here I used the electron- Builder for packing (my project was created based on Vue-CLI4 with vue-CLI-plugin-electron – Builder)

Therefore, in our packaging configuration file, we need to write the Protocols configuration

Note that the MAC and Windows protocols are separate.

Let’s explain it here

1. Just write here on a MAC. (Hands-on testing is feasible)

MAC protocol arousing, in essence, is the DMG installation file info.plist file under the URLType, do iOS development colleagues should have experience.

The path is /Application/ your Application right click to display the package Contents/ Contents/ info.plist

2.Windows requires additional scripting(At the time of writing, I had not yet tested Windows.)(Hands-on testing is feasible)

Windows needs to install the injection protocol by specifying the packaging method NSIS and injecting the executed SH script during packaging.

Another point to note here is that it is best to uninstall the corresponding protocol. (User experience first)

And then just pack it up

By now, your app should be aroused

SetAsDefaultProtocolClient is not need to write

How to evoke application testing?

Remember the name of the agreement you wrote? For example, I call it szyxqCA

Open a browser and type in the address box

szyxqca://
Copy the code

Can be

In theory, your browser will evoke your application

What if you want to pass?

szyxqca://startapp/here? xxx=123&xxx2=222Copy the code

Startapp /here is actually useless, mainly to add host and path and query, this format is more suitable for later extension

4. How to pass the parameters in this way and how to receive them?

Two life cycles are required

On ('second-instance', (event, argv) => {if (process.platform === 'win32') {logger.warn(' evoke '); logger.warn(event, argv); //TODO: not yet written}}); On ('open-url', (event, urlStr) => {logger.warn(' awaken '); logger.warn(event, urlStr); Const urlObj = new URL(urlStr); const { searchParams } = urlObj; // Data const XXX = searchparams.get (' XXX '); const xxx2 = searchParams.get('xxx2'); });Copy the code

5. Possible problems:

1. The app you wake up from is not correct

I wake up in the MAC app, has been puzzling aroused. / node_modules/electron/dist/electron. The app, and then I had deleted all node_modules, and to open the app, drum, normal.

DMG can be directly sent to colleagues when measured. After installation, DMG can be directly awakened without error.

Maybe it’s a bug in electron

Refer to the article

Electron arousing principle

The Electron application is aroused by both MAC/WIN registration protocols

To raise the sample

Configuration to view

The Electron application is aroused through a protocol

electron-builder