使用Url Schemes打开本地程序

测试:


将下列内容保存为test.html,用浏览器打开,单击“Test Url Schemes”超链接,即可看到效果(会启动相应的程序)


	
	
	
		Test Url Schemes
	




实现:


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文件

比如,修改前:





        NSPrincipalClass
        NSApplication
        CFBundleIconFile
        
        CFBundlePackageType
        APPL
        CFBundleGetInfoString
        Created by Qt/QMake
        CFBundleSignature
        ????
        CFBundleExecutable
        myapp
        CFBundleIdentifier
        com.yourcompany.myapp
        CFBundleDevelopmentRegion
        en
        NOTE
        This file was generated by Qt/QMake.


修改后:(增加“CFBundleURLTypes”等项)





        NSPrincipalClass
        NSApplication
        CFBundleIconFile
        
        CFBundlePackageType
        APPL
        CFBundleGetInfoString
        Created by Qt/QMake
        CFBundleSignature
        ????
        CFBundleExecutable
        myapp
        CFBundleIdentifier
        com.yourcompany.myapp
        CFBundleDevelopmentRegion
        en
        CFBundleURLTypes
        
                
                        CFBundleURLSchemes
                        
                                TestUrlSchemes
                        
                        CFBundleURLName
                        com.yourcompany.myapp
                
        
        NOTE
        This file was generated by Qt/QMake.




注意:第一次,需要手动打开程序(可能是因为程序不是从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






你可能感兴趣的:(笔记)