關鍵字:Browser,Application,URL Protocol,Windows,Mac,IE,Chrome,Safari。
OS: Windows 7, OS X Yosemite。
Windows:
1.寫一個C++ 應用程序,代碼如下:
#include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { if(argc > 1) { std::string argi(argv[1]); cout<<argi<<endl; } cout<<"Press any key to continue"<<endl; int i; cin>>i; return 0; }
2.在windows上新建文件MyApp.reg,文件內容如下:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\MyApp]
@="Open MyApp"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\MyApp\shell]
[HKEY_CLASSES_ROOT\MyApp\shell\open]
[HKEY_CLASSES_ROOT\MyApp\shell\open\command]
@="\"L:\\Dev\\MyApp\\Debug\\MyApp.exe\" \"%1\""
注:把"L:\\Dev\\MyApp\\Debug\\MyApp.exe\"更新為MyApp.exe所在的路徑。
在Windows上運行MyApp.reg添加內容到注冊表,如下圖:
4.寫一段HTML測試代碼如下,保存到文件MyAppTest.html:
<!DOCTYPE html> <html> <body> file URL or path: <input id='fileinput' type='text'/><br> <button type='button' onclick='window.location.href = "MyApp://command=open&file=" + encodeURIComponent(document.getElementById("fileinput").value)'>Open in MyApp</button> <button type='button' onclick='window.location.href = "MyApp://command=insert&file=" + encodeURIComponent(document.getElementById("fileinput").value)'>Insert in MyApp</button> </body> </html>
分別在Windows上用瀏覽器運行MyAppTest.html,點擊“Open in MyApp”,結果如下:
OSX:
用Xode打開你的project
1.選擇你的 project.
2.選擇你的 target.
3.打開 Info tab.
4.在URL Types下面添加新的URL.
如下圖:
在Safari里面運行URL:myapp://,將啟動這個應用。