最近有朋友問小編這樣一個問題,先給大家說下項目需求:掃描二維碼打開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 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 id="openApp">點擊打開</a><script type="text/javascript">//安卓// /**/window.location.;// 通過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.; }if(isAndroid){alert("請點擊右上角在瀏覽器打開");window.location.;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的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答