Javascript iOS9:如果可能,尝试通过方案打开应用程序,否则重定向到应用程序商店
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32689483/
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
iOS9: Try to open app via scheme if possible, or redirect to app store otherwise
提问by gran33
My question is about iOS9 only!
我的问题仅是关于iOS9 的!
I have an HTML
landing page, and I try to redirect the user to my appvia URL scheme if the app is installed, or redirect to the Appstoreotherwise.
我有一个HTML
登陆页面,如果安装了应用程序,我会尝试通过 URL 方案将用户重定向到我的应用程序,否则重定向到Appstore。
My code is:
我的代码是:
document.addEventListener("DOMContentLoaded", function(event) {
var body = document.getElementsByTagName('body')[0];
body.onclick = function () {
openApp();
};
});
var timeout;
function preventPopup() {
clearTimeout(timeout);
timeout = null;
window.removeEventListener('pagehide', preventPopup);
}
function openApp(appInstanceId, platform) {
window.addEventListener('pagehide', preventPopup);
document.addEventListener('pagehide', preventPopup);
// create iframe
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.setAttribute("style", "display:none;");
iframe.src = 'myscheme://launch?var=val';
var timeoutTime = 1000;
timeout = setTimeout(function () {
document.location = 'https://itunes.apple.com/app/my-app';
}, timeoutTime);
}
The problem is that the iframe
trick doesn't work in Safari
iOS9
.
问题是这个iframe
技巧在Safari
iOS9
.
Any idea why?
知道为什么吗?
My iframe
trick based on thisanswer.
我的iframe
技巧基于这个答案。
回答by st.derrick
The iframe trick no longer works -- my guess is that Apple knows it will encourage more developers to implement Universal Links, more quickly.
iframe 技巧不再有效——我猜苹果知道它会鼓励更多的开发人员更快地实现通用链接。
You can still set window.location='your-uri-scheme://';
and fallback to the App Store after 500ms. There is a "dance" between popups if you take this approach, as we do at Branch(we do as a fallback if Universal Links don't work).
您仍然可以window.location='your-uri-scheme://';
在 500 毫秒后设置并回退到 App Store。如果您采用这种方法,弹出窗口之间会发生“跳舞”,就像我们在Branch所做的那样(如果通用链接不起作用,我们会作为后备)。
window.location = 'your-uri-scheme://'; // will result in error message if app not installed
setTimeout(function() {
// Link to the App Store should go here -- only fires if deep link fails
window.location = "https://itunes.apple.com/us/app/myapp/id123456789?ls=1&mt=8";
}, 500);
I wish I had a better answer for you. iOS 9 is definitely more limited.
我希望我有一个更好的答案给你。iOS 9 肯定更受限制。
For a helpful overview of what's needed for Universal Links should you go that route, check out my answer hereor read this tutorial
回答by Bart Teeuwisse
As already mentioned setting window.location
on iOS 9 still works. However, this brings up an Open in App dialog. I've put an example on https://bartt.me/openappthat:
如前所述window.location
,iOS 9 上的设置仍然有效。但是,这会打开一个在应用程序中打开的对话框。我在https://bartt.me/openapp上举了一个例子:
- Launches Twitter when the Open in Twitter appis clicked.
- Falls back to the Twitter app in the App Store.
- Redirects to Twitter or the App Store withoutthe user selecting Openin the Open in Appdialog.
- Works in all browsers on iOS and Android.
- 单击“在 Twitter 中打开”应用程序时启动 Twitter 。
- 回到 App Store 中的 Twitter 应用程序。
- 重定向到 Twitter 或 App Store,而无需用户在“在应用程序中打开”对话框中选择“打开” 。
- 适用于 iOS 和 Android 上的所有浏览器。
Look at the source of https://lab.bartt.me/openappfor more information.
查看https://lab.bartt.me/openapp的来源了解更多信息。
回答by carlos.baeza
Maybe try giving you app support to Universal Links
也许尝试为您提供对通用链接的应用程序支持
Idea: Avoid custom (JavaScript, iframe) solutions in Safari, replace you code with a supported Universal Link.
想法:避免在 Safari 中使用自定义(JavaScript、iframe)解决方案,用受支持的通用链接替换您的代码。
Example
例子
<html>
<head>
...
</head>
<body>
<div class"app-banner-style">
<a href="http://yourdomain.com">In app open</a>
</div>
...content
</body>
</html>
if you app support Universal Links (e.g. yourdomain.com), you muss configure your domain (and path) and iOS9 should be react to it link opening you App. That is only theory, but I guess should be work :)
如果您的应用程序支持通用链接(例如 yourdomain.com),您必须配置您的域(和路径),iOS9 应该对它的链接做出反应,打开您的应用程序。这只是理论,但我想应该是可行的:)
回答by xac
iframe hack doesn't work in ios9 anymore. Possible solution is use two buttons. Example:
iframe hack 不再适用于 ios9。可能的解决方案是使用两个按钮。例子:
$('#goToStoreBtn').text( "go to store" ).click(function(event){
event.preventDefault();
event.stopPropagation();
window.location = storeUrl; // https://itunes.apple.com/...
});
$('#goToAppBtn').text( "go to app" ).click(function(event){
event.preventDefault();
event.stopPropagation();
window.location = appUrl; // myApp://...
});
回答by prabeen giri
This is relatively old thread, but I have created a library that supports deeplinking on most of the modern mobile browsers. But this requires separate deeplinking page which needs to be hosted in different domain to support universal linking in ios9 facebook browser.
这是一个相对较旧的线程,但我创建了一个支持大多数现代移动浏览器上的深度链接的库。但这需要单独的深层链接页面,该页面需要托管在不同的域中,以支持 ios9 facebook 浏览器中的通用链接。