實現網頁調起PC客戶端,打開會話聊天

需求:實現相似阿里旺旺,用戶在網頁點擊發起聊天的按鈕,需調起PC客戶端打開對應會話,如打開會話shell

實現原理:
window是把自定義協議寫入註冊表,打開對應exe程序
mac是在Info.plist文件添加CFBundleURLTypesapp

Window

咱們的程序是基於nsis製做的,須要在nsis腳本onInit函數裏添加寫註冊表邏輯函數

合併下面的到nsis的安裝腳本中,而後從新編譯code

注意:腳本中必須先設置了 $INSTDIR 變量事件

Function .onInit
  Var /GLOBAL protocol
  StrCpy $protocol "workplus"

  Var /GLOBAL app
  StrCpy $app "$INSTDIR\workplus\WorkPlus.exe" 

  SetRegView 64
  WriteRegStr HKCR "$protocol" "" "URL:$protocol Protocol"
  WriteRegStr HKCR "$protocol" "URL Protocol" ""
  WriteRegStr HKCR "$protocol\shell\open\command" "" "$\"$app$\" $\"%1$\""
  
  SetRegView 32
  WriteRegStr HKCR "$protocol" "" "URL:$protocol Protocol"
  WriteRegStr HKCR "$protocol" "URL Protocol" ""
  WriteRegStr HKCR "$protocol\shell\open\command" "" "$\"$app$\" $\"%1$\""
FunctionEnd

注意:這段代碼要放在install週期去執行,否則$INSTDIR默認是C盤開發

Mac

選擇app,右鍵顯示內容,編輯:Info.pliststring

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>workplus handler</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>workplus</string>
        </array>
    </dict>
</array>

workplus handler:爲handler的名字,能夠爲任意的值 it

workplus:爲protocol的名字,能夠爲任意的值io

傳參

程序是基於nwjs-0.12.3的版本開發的,每次喚起App都會觸發open或reopen事件,
捕獲事件後能夠經過函數參數或nw.App.argv[0]獲取外部參數編譯

坑: mac nwjs-0.12.3有bug, 不能獲取到參數,只能升級到0.13之後的版本

window nwjs-0.12.3 參數後面會帶多一個斜槓

網頁調用

<a href="workplus://xxxxx" >打開會話</a>

雙斜槓後面的是參數

相關文章
相關標籤/搜索