尝试打开未安装的本机应用程序时如何防止 iOS Safari 警报?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19701708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to prevent iOS safari alert when trying to open non-installed native app?
提问by Dima Feldman
I've been looking for a way to open a native iOS app from the browser. I found a decent solution here: Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?
我一直在寻找一种从浏览器打开本机 iOS 应用程序的方法。我在这里找到了一个不错的解决方案:Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?
This solution works great when you have the app installed. but when a user doesn't have this app installed - safari fires an error message which says "Safari cannot open the page because the address is invalid."
当您安装了该应用程序时,此解决方案非常有效。但是当用户没有安装这个应用程序时 - safari 会触发一条错误消息,上面写着“Safari 无法打开页面,因为地址无效。”
Is there a way to prevent this behaviour and instead to prompt the user to download the app?
有没有办法阻止这种行为,而是提示用户下载应用程序?
回答by ElizaS
Here is a solution that works for me:
这是一个对我有用的解决方案:
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);
}
回答by Kohei Iwasaki
use iframe.
使用 iframe。
$('<iframe />').attr('src', "appname://").attr('style', 'display:none;').appendTo('body');
回答by akruspe
For solving this and avoid no wanted iOS safari alert I've used a different approach handle also an iframe but without jquery and listener events. It works perfectly.
为了解决这个问题并避免不需要的 iOS Safari 警报,我使用了一种不同的方法来处理 iframe,但没有 jquery 和侦听器事件。它完美地工作。
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);
回答by Pablo Moretti
I'm using meta refresh as fallback because this works without javascript. This code works in iOS and Android.
我使用元刷新作为后备,因为这在没有 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>
回答by Patricia Hinn
For me this does the job:
对我来说,这可以完成工作:
window.open(appLink, '_system')`
window.open(appstoreLink, '_system')
Works for android and ios. If the first one can't be opened the second one is called without alert or something. Otherwise it just opens the app.
适用于安卓和ios。如果第一个无法打开,则在没有警报或其他情况下调用第二个。否则它只会打开应用程序。
回答by Thomas Deniau
The right way to do this is to use Smart App Banners: https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html
正确的做法是使用智能应用横幅:https: //developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html
If your app is not installed, the banner will let the user install it. If it is installed, it will open the app, and you have a way of specifying custom URL arguments to pass to the app.
如果您的应用未安装,横幅将让用户安装它。如果已安装,它将打开应用程序,并且您可以指定要传递给应用程序的自定义 URL 参数。