Protocol Handler

The app module provides methods for handling protocols.

These methods allow you to set and unset the protocols your app should be the default app for. Similar to when a browser asks to be your default for viewing web pages.

Open the full app API documentation(opens in new window) in your browser.

You can set your app as the default app to open for a specific protocol. For instance, in this demo we set this app as the default for electron-api-demos://. The demo button above will launch a page in your default browser with a link. Click that link and it will re-launch this app.

Packaging

This feature will only work on macOS 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 plist for the app is updated to include the new protocol handler. If you're using electron-packager then you can add the flag --extend-info with a path to the plist you've created. The one for this app is below.

Renderer Process

            const {shell} = require('electron')
            const path = require('path')
            const protocolHandlerBtn = document.getElementById('protocol-handler')
            protocolHandlerBtn.addEventListener('click', () => {
                const pageDirectory = __dirname.replace('app.asar', 'app.asar.unpacked')
                const pagePath = path.join('file://', pageDirectory, '../../sections/system/protocol-link.html')
                shell.openExternal(pagePath)
            })
          
Main Process

            const {app, dialog} = require('electron')
            const path = require('path')

            if (process.defaultApp) {
                if (process.argv.length >= 2) {
                    app.setAsDefaultProtocolClient('electron-api-demos', process.execPath, [path.resolve(process.argv[1])])
                }
            } else {
                app.setAsDefaultProtocolClient('electron-api-demos')
            }

            app.on('open-url', (event, url) => {
                dialog.showErrorBox('Welcome Back', `You arrived from: ${url}`)
            })

          
macOS plist

            
                
                    
                        
                            CFBundleURLTypes
                            
                                
                                    CFBundleURLSchemes
                                    
                                        electron-api-demos
                                    
                                    CFBundleURLName
                                    Electron API Demos Protocol
                                
                            
                            ElectronTeamID
                            VEKTX9H2N7