最近有朋友問小編這樣一個問題,先給大家說下項目需求:掃描二維碼開啟app如果使用者沒有這個app則提示它跳轉。
用網頁直接來調用app是不打可能的,必須原生那邊先做一些配置。
首先,安卓和蘋果的調用方法是不同的。
所以我們需要先判斷一下終端。
var u = navigator.userAgent,app = navigator.appVersion;var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android終端或者uc瀏覽器var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
之後最好是分別跳轉到兩個不同的頁面去做操作,因為蘋果需要在頭部配置一個app-id<meta name='apple-itunes-app' content='app-id=1115968341'>
下面是蘋果的代碼
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name='apple-itunes-app' content='app-id=1115968341'><title></title></head><body><a href="https://itunes.apple.com/cn/app/yi-fang-kuai-le-xue-tang/id1115968341?mt=8" id="openApp">點擊開啟</a><script type="text/javascript"> //蘋果document.getElementById('openApp').onclick = function(e){ // 通過iframe的方式試圖開啟APP,如果能正常開啟,會直接切換到APP,並自動阻止a標籤的預設行為 // 否則開啟a標籤的href連結 var ifr = document.createElement('iframe'); ifr.src = 'iosefunbox://'; ifr.style.display = 'none'; document.body.appendChild(ifr); window.setTimeout(function(){ document.body.removeChild(ifr); },3000) };</script></body></html>
這裡的ifr.src就是你去開啟app的協議路徑,安卓和蘋果是不一樣的。<br><br><br>如果是安卓機的話就簡單了
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no"></head><body><a href="http://a.app.qq.com/o/simple.jsp?pkgname=com.edufound.mobile" id="openApp">點擊開啟</a><script type="text/javascript">//安卓// /**/window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.edufound.mobile";// 通過iframe的方式試圖開啟APP,如果能正常開啟,會直接切換到APP,並自動阻止a標籤的預設行為 // 否則開啟a標籤的href連結 var ifr = document.createElement('iframe'); ifr.src = 'efunbox://efunbox.app/efunbox/open'; ifr.style.display = 'none'; document.body.appendChild(ifr); window.setTimeout(function(){ document.body.removeChild(ifr); },3000);</script></body></html>
這是我們原先的需求,後來變了,說蘋果直接跳轉到appstore裡面就好了,不用直接開啟,安卓的話需要直接開啟。
這樣我就直接把它們集合在一個網頁就行。
我上面的a連結是直接跳轉到騰訊應用寶裡面。
用網頁掃描訪問的話是沒問題的,
但是我就感覺會出事,後來拿微信掃一掃就蒙逼了。安卓只會開啟a連結,跳轉不進app,因為被微信攔截掉了,蘋果也一樣,解決的方案只能是點擊右上方,提示使用者在瀏覽器開啟就沒問題。這種方法是無奈之舉,但後來針對蘋果機找到了一個解決它的方案就是,a連結的跳轉直接跳騰訊應用寶上架的連結,然後微信內部會處理幫你自動跳轉到appstore裡面。
最後是整合了一下的代碼。
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body id="body"><script type="text/javascript">var u = navigator.userAgent,app = navigator.appVersion;var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android終端或者uc瀏覽器var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端if(isIOS){window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.edufound.mobile"; }if(isAndroid){alert("請點擊右上方在瀏覽器開啟");window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.edufound.mobile";var ifr = document.createElement('iframe'); ifr.src = 'efunbox://efunbox.app/efunbox/open'; ifr.style.display = 'none'; document.body.appendChild(ifr); window.setTimeout(function(){ document.body.removeChild(ifr); },3000);}</script></body></html>
補充:
掃描二維碼跳轉app
1、判斷是否安裝了app
<html> <head> <meta name="viewport" content="width=device-width" /> </head> <body> <h2><a id="applink1" href="mtcmtc://profile/116201417">Open scheme(mtcmtc) defined in iPhone with parameters </a></h2> <h2><a id="applink2" href="unknown://nowhere">open unknown with fallback to appstore</a></h2> <p><i>Only works on iPhone!</i></p> <script type="text/javascript"> // To avoid the "protocol not supported" alert, fail must open another app.var appstore = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";function applink(fail){return function(){var clickedAt = +new Date;// During tests on 3g/3gs this timeout fires immediately if less than 500ms.setTimeout(function(){// To avoid failing on return to MobileSafari, ensure freshness!if (+new Date - clickedAt < 2000){window.location = fail;}}, 500); };}document.getElementById("applink1").onclick = applink(appstore);document.getElementById("applink2").onclick = applink(appstore);</script> </body> </html>
2、開啟項目工程target裡面的schemurl追加://
以上所述是小編給大家介紹的通過掃描二維碼開啟app的實現代碼,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!