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.

    1. if (process.defaultApp) {
    2. if (process.argv.length >= 2) {
    3. app.setAsDefaultProtocolClient('electron-fiddle', process.execPath, [path.resolve(process.argv[1])])
    4. }
    5. } else {
    6. app.setAsDefaultProtocolClient('electron-fiddle')
    7. }

    We will now define the function in charge of creating our browser window and load our application’s index.html file.

    1. const createWindow = () => {
    2. // Create the browser window.
    3. width: 800,
    4. height: 600,
    5. webPreferences: {
    6. preload: path.join(__dirname, 'preload.js')
    7. }
    8. })
    9. }

    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:

    1. // This method will be called when Electron has finished
    2. // initialization and is ready to create browser windows.
    3. // Some APIs can only be used after this event occurs.
    4. app.whenReady().then(() => {
    5. createWindow()
    6. })
    7. // Handle the protocol. In this case, we choose to show an Error Box.
    8. app.on('open-url', (event, url) => {
    9. dialog.showErrorBox('Welcome Back', `You arrived from: ${url}`)

    Finally, we will add some additional code to handle when someone closes our application.

    1. // Quit when all windows are closed, except on macOS. There, it's common
    2. // for applications and their menu bar to stay active until the user quits
    3. // explicitly with Cmd + Q.
    4. app.on('window-all-closed', () => {
    5. if (process.platform !== 'darwin') app.quit()
    6. })

    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.

    1. const packager = require('electron-packager')
    2. packager({
    3. // ...other options...
    4. protocols: [
    5. {
    6. name: 'Electron Fiddle',
    7. schemes: ['electron-fiddle']
    8. }
    9. ]
    10. }).then(paths => console.log(`SUCCESS: Created ${paths.join(', ')}`))
    11. .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