我一直在寻找一种通过浏览器打开本机iOS应用的方法。我在这里找到了不错的解决方案:Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?
安装了应用程序后,此解决方案效果很好。但是当用户未安装此应用时-Safari会引发错误消息,提示“ Safari无法打开页面,因为地址无效。”
有没有一种方法可以防止这种行为,而是提示用户下载应用程序?
以下是对我有用的解决方案:
var timeout;
function preventPopup() {
clearTimeout(timeout);
timeout = null;
window.removeEventListener('pagehide', preventPopup);
}
function openApp() {
$('<iframe />')
.attr('src', appurl)
.attr('style', 'display:none;')
.appendTo('body');
timeout = setTimeout(function() {
document.location = appstore;
}, 500);
window.addEventListener('pagehide', preventPopup);
}
使用iframe。
$('<iframe />').attr('src', "appname://").attr('style', 'display:none;').appendTo('body');
为了解决这个问题,避免了没有想要的iOS野生动物园警报,我使用了另一种方法来处理iframe,但没有jquery和listener事件。它运行完美。
var iframe = document.createElement("iframe");
iframe.style.border = "none";
iframe.style.width = "1px";
iframe.style.height = "1px";
iframe.onload = function () {
document.location = alt;
};
iframe.src = nativeSchemaUrl; //iOS app schema url
window.onload = function(){
document.body.appendChild(iframe);
}
setTimeout(function(){
window.location = fallbackUrl; //fallback url
},300);
我正在使用元刷新作为后备,因为这种方法无需使用JavaScript。此代码可在iOS和Android中使用。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="1; url=http://www.facebook.com">
<title>Redirecting..</title>
<script>
function tryOpenApp() {
var iframe = document.createElement("iframe");
iframe.style.border = "none";
iframe.style.width = "1px";
iframe.style.height = "1px";
iframe.src = "fb://feed/";
document.body.appendChild(iframe);
}
</script>
</head>
<body onload="tryOpenApp()">
</body>
</html>
对我来说,这可以完成工作:
window.open(appLink,'_system')
window.open(appstoreLink,'_system')
适用于android和ios。如果第一个无法打开,则第二个将在没有警报或其他提示的情况下被调用。否则,它将仅打开该应用程序。
如果未安装您的应用程序,则横幅广告将允许用户安装它。如果已安装,它将打开应用程序,您可以指定自定义URL参数以传递给应用程序。