話很少說直接奔入主題,瞭解了使用hybrid的緣由,以及更新上線流程,咱們在這裏再說一下 js 如何與客戶端通訊。。安全
使用:NgariHealth://直接打開納裏健康 app網絡
void function (window, undefined) { //調用 schema 協議 function _invoke(action, data, callback) { //拼接 schema 協議 var schema = 'myapp://utils/' + action //拼接參數 schema += '?a=a'; for (const key in data) { if (data.hasOwnProperty(key)) { const element = data[key]; schema += '&' + key + '=' + element; } } // 處理 callback var callbackName = ''; if (typeof callback === 'string') { callbackName = callback; } else { callbackName = action + Date.now(); window[callbackName] = callback; } schema += 'callback=' + callbackName; var iframe = document.createElement('iframe'); iframe.src = schema; var body = document.body; body.appendChild(iframe); setTimeout(()=>{ body.removeChild(iframe); iframe = null; }) } //暴露到全局變量 window.invoke = { share: function (data, callback) { _invoke('share', data, callback); }, scan: function (data, callback) { _invoke('share', data, callback); }, login: function (data, callback) { _invoke('share', data, callback); }, } }(window)