Last active
March 5, 2018 09:57
-
-
Save mirinzhang/a9aad356fc91fa9d169428bcc2b93497 to your computer and use it in GitHub Desktop.
使用Js打开或者下载App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param microLink 应用宝微下载链接 | |
* @param schemeUrl 需要打开的schemeUrl | |
* @param downloadUrl App通用下载地址 | |
*/ | |
function openOrDownloadApp(microLink, schemeUrl, downloadUrl) { | |
// 收集设备信息 | |
var deviceInfo = { | |
userAgent: navigator.userAgent.toLowerCase(), | |
isAndroid: !!navigator.userAgent.match(/android/ig), | |
isIphone: !!navigator.userAgent.match(/iphone|ipod/ig), | |
isIpad: !!navigator.userAgent.match(/ipad/ig), | |
isWeixin: !!navigator.userAgent.match(/MicroMessenger/ig), | |
}; | |
// 如果是在微信中打开,则使用微下载进行打开或下载APP | |
if(deviceInfo.isWeixin) { | |
window.location.href = microLink + '&android_schema=' + window.encodeURI(schemeUrl); | |
return; | |
} | |
// 非微信环境,先尝试打开App | |
var iframe = document.createElement('iframe'); | |
iframe.src = schemeUrl; | |
iframe.style.display = 'none'; | |
document.body.appendChild(iframe); | |
// 若2秒内未打开App则说明未安装App,跳转到App下载页面 | |
window.setTimeout(function() { | |
document.body.removeChild(iframe); | |
window.location.href = downloadUrl; | |
}, 2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment