Deep Links
By the end of this tutorial, we will have set our app to intercept and handle any clicked URLs that start with a specific protocol. In this guide, the protocol we will use will be ““.
First, we will import the required modules from electron
. These modules help
control our application lifecycle and create a native browser window.
Next, we will proceed to register our application to handle all “electron-fiddle://
“ protocols.
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient('electron-fiddle', process.execPath, [path.resolve(process.argv[1])])
}
} else {
app.setAsDefaultProtocolClient('electron-fiddle')
}
We will now define the function in charge of creating our browser window and load our application’s index.html
file.
const createWindow = () => {
// Create the browser window.
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
}
This code will be different in Windows compared to MacOS and Linux. This is due to Windows requiring additional code in order to open the contents of the protocol link within the same Electron instance. Read more about this here.
Windows code:
MacOS and Linux code:
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
})
// Handle the protocol. In this case, we choose to show an Error Box.
app.on('open-url', (event, url) => {
dialog.showErrorBox('Welcome Back', `You arrived from: ${url}`)
Finally, we will add some additional code to handle when someone closes our application.
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
Packaging
On macOS and Linux, this feature will only work when your app is packaged. It will not work when
you’re launching it in development from the command-line. When you package your app you’ll need to
make sure the macOS Info.plist
and the Linux files for the app are updated to include
the new protocol handler. Some of the Electron tools for bundling and distributing apps handle
this for you.
If you’re using Electron Forge, adjust packagerConfig
for macOS support, and the configuration for
the appropriate Linux makers for Linux support, in your Forge
configuration (please note the following example only
shows the bare minimum needed to add the configuration changes):
Electron Packager
If you’re using Electron Packager’s API, adding support for protocol handlers is similar to how
Electron Forge is handled, except
protocols
is part of the Packager options passed to the packager
function.
const packager = require('electron-packager')
packager({
// ...other options...
protocols: [
{
name: 'Electron Fiddle',
schemes: ['electron-fiddle']
}
]
}).then(paths => console.log(`SUCCESS: Created ${paths.join(', ')}`))
.catch(err => console.error(`ERROR: ${err.message}`))
If you’re using Electron Packager’s CLI, use the --protocol
and --protocol-name
flags. For
example:
After you start your Electron app, you can enter in a URL in your browser that contains the custom
protocol, for example "electron-fiddle://open"
and observe that the application will respond and
show an error dialog box.
```fiddle docs/fiddles/system/protocol-handler/launch-app-from-URL-in-another-app