DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 通過掃描二維碼打開app的實現代碼
通過掃描二維碼打開app的實現代碼
編輯:關於JavaScript     

最近有朋友問小編這樣一個問題,先給大家說下項目需求:掃描二維碼打開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的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved