網頁調起APP 兼容IOS 9以上

網頁源碼 注意將url作爲參數放在自定義scheme中傳遞時,要使用base64編碼,避免出現混亂

<!doctype html>
<html>
  
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>打開我的app</title>
    <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui"></head>
  
  <body>
    <div>
      <a id="J-call-app" href="javascript:;" class="label">立即打開>></a>
      <input id="J-download-app" type="hidden" name="storeurl" value=""></div>
    <script>(function() {
        var ua = navigator.userAgent.toLowerCase();
        var t;
        var doc_id=805050;
        var doc_type=2;
        var doc_url=btoa('https://appexternal.cnjxol.com:81/webDetails/news?id=805050&tenantId=1')//用base64編碼url;
        var config = {
          /*scheme:必須*/
          scheme_IOS: 'jxrb-dujia://action=openNews?id='+doc_id+'&doc_type='+doc_type+'&url='+doc_url,
          scheme_Adr: 'jxrb-dujia://splash?id='+doc_id+'&doc_type='+doc_type+'&url='+doc_url,
          ios_download_url: 'https://itunes.apple.com/cn/app/id1475721349?mt=8',
          adr_download_url: 'https://appexternal.cnjxol.com:81/webDetails/download.html?tenant_id=1',
          timeout: 600
        };

        function openclient() {
          var startTime = Date.now();

          var ifr = document.createElement('iframe');

          ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS: config.scheme_Adr;
          ifr.style.display = 'none';
          document.body.appendChild(ifr);

          var t = setTimeout(function() {
            var endTime = Date.now();

            if (!startTime || endTime - startTime < config.timeout + 200) {
              var is_ios = ua.indexOf('os') > 0;
              var download_url = is_ios ? config.ios_download_url: config.adr_download_url;
              var location=window.location;
              
              if (is_ios) {
                location.href = config.scheme_IOS;
                  setTimeout(function() {
                  location.href = download_url;
                },
                250);

                setTimeout(function() {
                  location.reload();
                },
                1000);
              } else {
                window.location = download_url;
              }

            }
          },
          config.timeout);

          window.onblur = function() {
            clearTimeout(t);
          }
        }
        window.addEventListener("DOMContentLoaded",
        function() {
          document.getElementById("J-call-app").addEventListener('click', openclient, false);

        },
        false);
      })()</script>
  </body>

</html>

Android中在需要打開的activity中註冊
scheme和host要和網頁中設置的一致

   <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:host="splash" android:scheme="jxrb-dujia" />
            </intent-filter>

在activity中獲取數據

 Uri uri = getIntent().getData();
        if (uri != null) {
            String id = uri.getQueryParameter("id");
            String doc_type = uri.getQueryParameter("doc_type");
            String url = new String(Base64.decode(uri.getQueryParameter("url"), 0));
            Log.i("ttt","url="+url);
            extras = new Bundle();
            extras.putInt(MainActivity.KEY_DOC_ID, Integer.parseInt(id));
            extras.putInt(MainActivity.KEY_DOC_TYPE, Integer.parseInt(doc_type));
            extras.putString(MainActivity.KEY_DOC_URL, url);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章