深层链接与协议处理器
让用户能够通过自定义 URL 方案启动并导航到您的 Electron 应用。这是关键的生命周期集成,可与启动事件和更新机制相互配合。
深层链接与协议处理器 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Launching From a Link
Deep linking lets a URL like myapp://open/project/42 launch or focus your app and jump straight to content.
Custom Protocols
Register a custom URL scheme so the OS knows links of that type belong to your app.
app.setAsDefaultProtocolClient('myapp');Parsing the Deep Link
Extract the path and parameters from the incoming URL to decide where to navigate.
function parseDeepLink(url) {
const u = new URL(url);
return { host: u.host, path: u.pathname };
}
console.log(parseDeepLink('myapp://open/project/42'));Handling Links on macOS
On macOS, deep links arrive via the open-url event, even while the app is running.
app.on('open-url', (event, url) => {
event.preventDefault();
handleLink(url);
});Handling Links on Windows and Linux
On Windows and Linux the URL comes in as a command-line argument, so inspect process.argv on launch.
function urlFromArgv(argv) {
return argv.find(a => a.startsWith('myapp://'));
}
console.log(urlFromArgv(['electron', '.', 'myapp://open/5']));Single Instance Coordination
Use the single instance lock so a second launch forwards its URL to the already-running app instead of starting a new one.
const gotLock = app.requestSingleInstanceLock();
if (!gotLock) app.quit();second-instance Event
The second-instance event delivers the new command line, where the deep link lives, and lets you focus the main window.
app.on('second-instance', (e, argv) => {
const url = urlFromArgv(argv);
if (url) handleLink(url);
});Cold Start vs Warm Start
Handle both cases: the app launched fresh by the link (cold) and the app already open (warm). Route both to the same handler.
Validating the Link
Never trust a deep link blindly. Validate the path and parameters before acting on them to avoid abuse.
Testing Deep Links
Test by typing the URL into a browser or terminal. During development the scheme must point at your packaged or running app.
Use Cases
Deep links power OAuth callbacks, sharing, and notifications that jump straight to relevant content, tying nicely into the app lifecycle.
Quick Check
Test your deep linking knowledge.
Recap
You learned to register a custom protocol, parse deep links, handle them across platforms, coordinate with the single-instance lock, and validate input, enabling URL-driven app navigation.
常见问题解答
「深层链接与协议处理器」课时是免费的吗?
是的 — 「深层链接与协议处理器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。
「深层链接与协议处理器」这节课中我会学到什么?
让用户能够通过自定义 URL 方案启动并导航到您的 Electron 应用。这是关键的生命周期集成,可与启动事件和更新机制相互配合。 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Electron Desktop App Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「深层链接与协议处理器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Electron Desktop App Development 课中编写并运行代码吗?
能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 管理应用生命周期事件
- 实现自动更新
- 崩溃报告
- 深层链接与协议处理器