I'm developing an app in Electron and I need to handle a custom protocol inside this app.
I'm using app.setAsDefaultProtocolClient(PROTOCOL)
for that.
I am using "open-url" for macOS to handle the URL with my custom protocol and it is working smoothly but I can't figure it out on Windows. I'm sending some data in the URL so just opening the window won't work.
I checked this answer, but this was answered in 2016 and the method makeSingleInstance
is now deprecated. In the docs, it suggests to use requestSingleInstanceLock
but it doesn't take any callbacks or return URL.
So how can I enable the same feature in both macOS and Windows?
Code
index.js
app.on('ready', () => createWindow(`file://${__dirname}/views/wele.html`));
app.on('activate', () => {
// eslint-disable-next-line no-shadow,global-require
const { mainWindow } = require('./utils/createWindow');
// On OS X it's mon to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow(`file://${__dirname}/views/wele.html`);
}
});
app.on('open-url', handleOpenURL);
app.setAsDefaultProtocolClient(PROTOCOL);
handleOpenURL.js
module.exports = (e, data) => {
e.preventDefault();
// Some other Logic
createWindow(URL);
}
I'm developing an app in Electron and I need to handle a custom protocol inside this app.
I'm using app.setAsDefaultProtocolClient(PROTOCOL)
for that.
I am using "open-url" for macOS to handle the URL with my custom protocol and it is working smoothly but I can't figure it out on Windows. I'm sending some data in the URL so just opening the window won't work.
I checked this answer, but this was answered in 2016 and the method makeSingleInstance
is now deprecated. In the docs, it suggests to use requestSingleInstanceLock
but it doesn't take any callbacks or return URL.
So how can I enable the same feature in both macOS and Windows?
Code
index.js
app.on('ready', () => createWindow(`file://${__dirname}/views/wele.html`));
app.on('activate', () => {
// eslint-disable-next-line no-shadow,global-require
const { mainWindow } = require('./utils/createWindow');
// On OS X it's mon to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow(`file://${__dirname}/views/wele.html`);
}
});
app.on('open-url', handleOpenURL);
app.setAsDefaultProtocolClient(PROTOCOL);
handleOpenURL.js
module.exports = (e, data) => {
e.preventDefault();
// Some other Logic
createWindow(URL);
}
Share
Improve this question
edited Oct 30, 2019 at 10:29
Apal Shah
asked Oct 30, 2019 at 10:09
Apal ShahApal Shah
7008 silver badges17 bronze badges
6
- 2 I think there is no tutorial available for windows to handle the same situation. You can register the protocol. and look for the process arguements to handle this. using process.argv . – Sharvin K Commented Oct 30, 2019 at 10:42
- 2 Also you can try this event for the same, github./electron/electron/blob/master/docs/api/… – Sharvin K Commented Oct 30, 2019 at 10:46
-
I tried this example: electronjs/docs/all#apprequestsingleinstancelock. I tried printing
event, mandLine, workingDirectory
, but this event is never getting executed. – Apal Shah Commented Oct 30, 2019 at 11:08 - ok then you can try using the process.argv to get the passed argements – Sharvin K Commented Oct 30, 2019 at 11:10
- Not able to figure out which event can handle a newly opened window where I can use process.argv – Apal Shah Commented Oct 30, 2019 at 11:20
1 Answer
Reset to default 8Take a look at this example it is built with angular and electron.
You just need to make sure of the following for the custom uri to work on windows:
First, only one instant is running, by checking app.requestSingleInstanceLock()
if it is true then you need to quit the app app.quit()
. because we only need one instance to run. Second, you should handle the second-instance
event app.on('second-instance', (event, args) => {})
const customSchemeName = 'x-pany-app';
const primaryInstance = app.requestSingleInstanceLock();
if (!primaryInstance) {
app.quit();
return;
}
// The primary instance of the application will run this code, not the new instance
app.on('second-instance', (event, args) => {
// handle custom uri
}
...
// Register private URI scheme for the current user when running for the first time
app.setAsDefaultProtocolClient(customSchemeName);
// Handle custom uri requests against the running app on Mac OS
app.on('open-url', (event, customSchemeData) => {
event.preventDefault();
// handle the data
});
...
I had the same issue on windows, this fixed it for me, I have tested it and it works. credits goes to gary archer.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742285396a4415230.html
评论列表(0条)