自定义协议方案关联
自定义协议功能允许您将特定的自定义协议与您的应用程序关联,以便当用户打开使用此协议的链接时,您的应用程序会启动以处理它们。这对于将您的桌面应用程序与您的 Web 应用程序连接特别有用。在本指南中,我们将逐步介绍在 Wails 应用程序中实现自定义协议的步骤。
设置自定义协议方案关联:
要设置自定义协议,您需要修改应用程序的 wails.json 文件。在“info”部分添加一个“protocols”部分,指定您的应用程序应该关联的协议。
例如
{
"info": {
"protocols": [
{
"scheme": "myapp",
"description": "My App Protocol",
"role": "Editor"
}
]
}
}
属性 | 描述 |
---|---|
scheme | 自定义协议方案。例如 myapp |
description | 仅限 Windows。描述。 |
role | 仅限 macOS。应用程序相对于类型的角色。对应于 CFBundleTypeRole。 |
平台特定:
macOS
当您使用您的应用程序打开自定义协议时,系统将启动您的应用程序并调用 Wails 应用程序中的 OnUrlOpen
函数。示例
main.go
func main() {
// Create application with options
err := wails.Run(&options.App{
Title: "wails-open-file",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
Mac: &mac.Options{
OnUrlOpen: func(url string) { println(url) },
},
Bind: []interface{}{
app,
},
})
if err != nil {
println("Error:", err.Error())
}
}
Windows
在 Windows 上,自定义协议方案仅在使用 NSIS 安装程序时受支持。在安装过程中,安装程序将为您的方案创建注册表项。当您使用您的应用程序打开 URL 时,会启动应用程序的新实例,并且 URL 将作为参数传递给您的应用程序。为了处理这一点,您应该在您的应用程序中解析命令行参数。示例
main.go
func main() {
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) != 0 {
println("launchArgs", argsWithoutProg)
}
}
您还可以为您的应用程序启用单实例锁定。在这种情况下,当您使用您的应用程序打开 URL 时,不会启动应用程序的新实例,并且参数将传递给已运行的实例。有关详细信息,请查看单实例锁定指南。示例
main.go
func main() {
// Create application with options
err := wails.Run(&options.App{
Title: "wails-open-file",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "e3984e08-28dc-4e3d-b70a-45e961589cdc",
OnSecondInstanceLaunch: app.onSecondInstanceLaunch,
},
Bind: []interface{}{
app,
},
})
}
Linux
目前,Wails 不支持 Linux 的捆绑。因此,您需要手动创建文件关联。例如,如果您将您的应用程序作为 .deb 包分发,您可以通过在捆绑包中添加所需的文件来创建文件关联。您可以使用 nfpm 为您的应用程序创建 .deb 包。
- 为您的应用程序创建一个 .desktop 文件,并在其中指定文件关联(注意
%u
在 Exec 中很重要)。示例
[Desktop Entry]
Categories=Office
Exec=/usr/bin/wails-open-file %u
Icon=wails-open-file.png
Name=wails-open-file
Terminal=false
Type=Application
MimeType=x-scheme-handler/myapp;
- 为您的包准备 postInstall/postRemove 脚本。示例
# reload desktop database to load app in list of available
update-desktop-database /usr/share/applications
- 配置 nfpm 以使用您的脚本和文件。示例
name: "wails-open-file"
arch: "arm64"
platform: "linux"
version: "1.0.0"
section: "default"
priority: "extra"
maintainer: "FooBarCorp <[email protected]>"
description: "Sample Package"
vendor: "FooBarCorp"
homepage: "http://example.com"
license: "MIT"
contents:
- src: ../bin/wails-open-file
dst: /usr/bin/wails-open-file
- src: ./main.desktop
dst: /usr/share/applications/wails-open-file.desktop
- src: ../appicon.svg
dst: /usr/share/icons/hicolor/scalable/apps/wails-open-file.svg
# copy icons to Yaru theme as well. For some reason Ubuntu didn't pick up fileicons from hicolor theme
- src: ../appicon.svg
dst: /usr/share/icons/Yaru/scalable/apps/wails-open-file.svg
scripts:
postinstall: ./postInstall.sh
postremove: ./postRemove.sh
- 使用 nfpm 构建您的 .deb 包
nfpm pkg --packager deb --target .
- 现在,当您的包安装后,您的应用程序将与自定义协议方案关联。当您使用您的应用程序打开 URL 时,会启动应用程序的新实例,并且文件路径将作为参数传递给您的应用程序。为了处理这一点,您应该在您的应用程序中解析命令行参数。示例
main.go
func main() {
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) != 0 {
println("launchArgs", argsWithoutProg)
}
}
您还可以为您的应用程序启用单实例锁定。在这种情况下,当您使用您的应用程序打开 URL 时,不会启动应用程序的新实例,并且参数将传递给已运行的实例。有关详细信息,请查看单实例锁定指南。示例
main.go
func main() {
// Create application with options
err := wails.Run(&options.App{
Title: "wails-open-file",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "e3984e08-28dc-4e3d-b70a-45e961589cdc",
OnSecondInstanceLaunch: app.onSecondInstanceLaunch,
},
Bind: []interface{}{
app,
},
})
}