亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

通過掃描二維碼打開app的實現(xiàn)代碼

 更新時間:2016年11月10日 17:06:31   作者:DOM先生  
在項目開發(fā)中遇到這樣的需求,掃描二維碼打開app如果用戶沒有這個app則提示它跳轉(zhuǎn),怎么實現(xiàn)呢?下面小編給大家分享通過掃描二維碼打開app的實現(xiàn)代碼,感興趣的朋友參考下吧

最近有朋友問小編這樣一個問題,先給大家說下項目需求:掃描二維碼打開app如果用戶沒有這個app則提示它跳轉(zhuǎn)。

用網(wǎng)頁直接來調(diào)用app是不打可能的,必須原生那邊先做一些配置。

首先,安卓和蘋果的調(diào)用方法是不同的。

所以我們需要先判斷一下終端。

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終端

之后最好是分別跳轉(zhuǎn)到兩個不同的頁面去做操作,因為蘋果需要在頭部配置一個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的協(xié)議路徑,安卓和蘋果是不一樣的。<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>

這是我們原先的需求,后來變了,說蘋果直接跳轉(zhuǎn)到appstore里面就好了,不用直接打開,安卓的話需要直接打開。

這樣我就直接把它們集合在一個網(wǎng)頁就行。

我上面的a鏈接是直接跳轉(zhuǎn)到騰訊應用寶里面。

用網(wǎng)頁掃描訪問的話是沒問題的,

但是我就感覺會出事,后來拿微信掃一掃就蒙逼了。安卓只會打開a鏈接,跳轉(zhuǎn)不進app,因為被微信攔截掉了,蘋果也一樣,解決的方案只能是點擊右上角,提示用戶在瀏覽器打開就沒問題。這種方法是無奈之舉,但后來針對蘋果機找到了一個解決它的方案就是,a鏈接的跳轉(zhuǎn)直接跳騰訊應用寶上架的鏈接,然后微信內(nèi)部會處理幫你自動跳轉(zhuǎn)到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>

補充:

掃描二維碼跳轉(zhuǎn)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的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論