1. 程式人生 > >與app的互動

與app的互動

export const connectWebViewJavascriptBridge = (callback) => {

  if (window.WebViewJavascriptBridge) {

    callback(WebViewJavascriptBridge)
  } else {
    document.addEventListener('WebViewJavascriptBridgeReady', function () {
      callback(WebViewJavascriptBridge)
    }, false)
  }
};

export const setupWebViewJavascriptBridge=(callback) =>{
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'https://__bridge_loaded__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
};

export const isSony= () => {
  return /sonyapp/i.test(navigator.userAgent)
};

export const isSonyAppwk= () => {
  return /sonyapp_wkwebview/i.test(navigator.userAgent)
};
export const isSonyApp = () => {
  return isSony()||isSonyAppwk();
};

export const isApple = () => {
  return /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent);

};

export const isAndroid = () => {
  return /(Android)/i.test(navigator.userAgent);
};


export const doAppAction = (ed) => {
  try {
    if (isApple()) {
   if(isSonyAppwk()){
    console.log('sonyappwk');
        window.webkit.messageHandlers.showName.postMessage(ed);
      }else if(isSony()){
    connectWebViewJavascriptBridge((bridge) => {
     console.log("isApple-bridge:",bridge);
     bridge.send(JSON.stringify(ed), function (response) {
     console.log("bridge-response:",response);
     })
    });
      }
 
   }
   
    if (isAndroid() && ed.linktype) {
      window.SysClientJs.linktype(ed.linktype,JSON.stringify(ed));

    }

  } catch (err) {
    console.log("do app linktype"+err);
  }
};