添加scheme
在manifest.json文件中添加IOS及安卓的scheme節(jié)點(diǎn)
//IOS
"apple": {
"appid": "testguangdian.dome",/*iOS必選,蘋(píng)果開(kāi)發(fā)網(wǎng)站申請(qǐng)的appid,如io.dcloud.HelloMUI*/
"mobileprovision": "",/*iOS必選,打包配置文件*/
"password": "goncenleo01",/*iOS必選,導(dǎo)入配置文件密碼*/
"p12": "",/*iOS必選,打包配置文件關(guān)聯(lián)的個(gè)人證書(shū)*/
"devices": "universal",/*iphone,可取值iphone/ipad/universal*/
"urltypes":[
{
"urlidentifier":"testguangdian.dome",
"urlschemes":[
"zhsq"
]
}
],
"frameworks": []
},
//安卓
"google": {
"packagename": "",/*Android必選,程序包名,如io.dcloud.HelloMUI*/
"keystore": "",/*Android必選,打包證書(shū)文件*/
"password": "",/*Android必選,打包證書(shū)密碼*/
"aliasname": "",/*Android必選,打包證書(shū)別名*/
"schemes":[
"zhsq"
],
}
在APP的首頁(yè)中 做出響應(yīng)
document.addEventListener('plusready', function () {
checkArguments();
}, false);
// 判斷啟動(dòng)方式
function checkArguments() {
alert(plus.runtime.launcher )
if(plus.runtime.launcher == "default"){
return
}else if(plus.runtime.launcher == "scheme"){
var args = plus.runtime.arguments;
alert(JSON.stringify(args))
if (args) {
arr = args.split("?")[1];
var goodsid = arr.split("=")[1];
setTimeout(function(){
mui.openWindow("goods_detail.html?goodsid="+goodsid);
},1000)
}
}
}
// 處理從后臺(tái)恢復(fù)
document.addEventListener('newintent', function () {
console.log("addEventListener: newintent");
checkArguments();
}, false);
在H5中喚起
function gp_down(dev){ //下載鏈接
if(dev=='android'){
var ifr = document.createElement('iframe');
ifr.src = 'https://zhapp.4hl.cn/static/home/js/H53D548C0_0806114840.apk';
ifr.style.display = 'none';
document.body.appendChild(ifr);
}else if(dev=='ios'){
}
}
function submitFn(){
//判斷瀏覽器
var u = navigator.userAgent;
if(/MicroMessenger/gi.test(u)) {
// 引導(dǎo)用戶(hù)在瀏覽器中打開(kāi)
alert('請(qǐng)?jiān)跒g覽器中打開(kāi)');
return;
}
var d = new Date();
var t0 = d.getTime();
if(u.indexOf('Android') > -1 || u.indexOf('Linux') > -1){
//Android
var href = "zhsq://startapp?goodsid=428045"
// alert("Android-->zhsq")
// alert(openApp('zhsq://startapp'))
if(openApp(href)){
openApp(href);
}else{
//由于打開(kāi)需要1~2秒,利用這個(gè)時(shí)間差來(lái)處理--打開(kāi)app后,返回h5頁(yè)面會(huì)出現(xiàn)頁(yè)面變成app下載頁(yè)面,影響用戶(hù)體驗(yàn)
var delay = setInterval(function(){
var d = new Date();
var t1 = d.getTime();
if( t1-t0<3000 && t1-t0>2000){
// alert('請(qǐng)下載APP');
// window.location.href = " app下載地址 ";
}
if(t1-t0>=3000){
clearInterval(delay);
}
},1000);
}
}else if(u.indexOf('iPhone') > -1){
//IOS
// alert("ios-->zhsq")
// alert(openApp('zhsq://startapp'))
if(openApp('zhsq://startapp')){
openApp('zhsq://startapp');
}else{
var delay = setInterval(function(){
var d = new Date();
var t1 = d.getTime();
if( t1-t0<3000 && t1-t0>2000){
// alert('請(qǐng)下載APP');
// window.location.href = "app下載地址 ";
}
if(t1-t0>=3000){
clearInterval(delay);
}
},1000);
}
}
}
function openApp(src) {
// 通過(guò)iframe的方式試圖打開(kāi)APP,如果能正常打開(kāi),會(huì)直接切換到APP,并自動(dòng)阻止a標(biāo)簽的默認(rèn)行為
// 否則打開(kāi)a標(biāo)簽的href鏈接
var ifr = document.createElement('iframe');
// alert("openApp-->"+src)
ifr.src = src;
ifr.style.display = 'none';
document.body.appendChild(ifr);
window.setTimeout(function( ){
document.body.removeChild(ifr);
},2000);
}
submitFn()

H5中喚起APP