测试:
将下列内容保存为test.html,用浏览器打开,单击“Test Url Schemes”超链接,即可看到效果(会启动相应的程序)
<HTML>
<head>
</head>
<body>
<a href="TestUrlSchemes://">Test Url Schemes</a>
</body>
</HTML>
实现:
windows下:
只要向注册表的HKEY_CLASSES_ROOT中写入指定格式的信息,即可
用一个bat来快速说明:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\TestUrlSchemes]
@="Test URL Schemes"
"URL Protocol"="c:\\myapp.exe"
[HKEY_CLASSES_ROOT\TestUrlSchemes\DefaultIcon]
@="c:\\myapp.exe,1"
[HKEY_CLASSES_ROOT\TestUrlSchemes\shell]
[HKEY_CLASSES_ROOT\TestUrlSchemes\shell\open]
[HKEY_CLASSES_ROOT\TestUrlSchemes\shell\open\command]
@="\"c:\\myapp.exe\" \"%1\""
其中“TestUrlSchemes”这个可以改成你要取的自定义协议名称(貌似不能有特殊字符,且windows下会忽略大小写),
“c:\\myapp.exe”则可改成安装的程序的绝对路径,就是这么简单
mac下:
更简单,只需修改Contents下的Info.plist文件
比如,修改前:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleGetInfoString</key>
<string>Created by Qt/QMake</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleExecutable</key>
<string>myapp</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.myapp</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>NOTE</key>
<string>This file was generated by Qt/QMake.</string>
</dict>
</plist>
修改后:(增加“CFBundleURLTypes”等项)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleGetInfoString</key>
<string>Created by Qt/QMake</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleExecutable</key>
<string>myapp</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.myapp</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>TestUrlSchemes</string>
</array>
<key>CFBundleURLName</key>
<string>com.yourcompany.myapp</string>
</dict>
</array>
<key>NOTE</key>
<string>This file was generated by Qt/QMake.</string>
</dict>
</plist>
注意:第一次,需要手动打开程序(可能是因为程序不是从AppStore下载的,需要右键打开程序),之后,就可以直接用自定义协议的方式打开了
linux下:
在软件对应的.desktop文件增加字段“MimeType=x-scheme-handler/testurlschemes”
注意:
1、testurlschemes必须纯英文的小写
2、ubuntu14.04已经支持,centos6.8也支持(centos6.5经测试不支持)
3、.desktop文件放在/usr/share/applications/目录下(如果是手动改这个文件,执行update-desktop-database立即生效)
test.desktop文件内容大致如下:
[Desktop Entry]
GenericName=web transmit adapter
Name=web_transmit_adapter
Version=1.0.0
Exec=bash /usr/bin/web_transmit_adapter/web_transmit_adapter.sh
Comment=web transmit adapter on Linux x64
Icon=web_transmit_adapter.png
Type=Application
Terminal=false
StartupNotify=false
Encoding=UTF-8
Categories=Utility
MimeType=x-scheme-handler/testurlschemes