我的问题仅关于 iOS9!
我有一个
HTML
登陆页面,如果安装了应用程序,我尝试通过 URL 方案将用户 重定向到我的应用程序,否则重定向到 Appstore。
我的代码是:
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);
}
问题是
iframe
技巧在Safari
iOS9
中不起作用。
知道为什么吗?
我的
iframe
技巧基于this答案。
iframe 技巧不再有效——我的猜测是苹果知道它将鼓励更多开发者更快地实现通用链接。
您仍然可以设置
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);
我希望能为您提供更好的答案。 iOS 9 肯定受到更多限制。
正如已经提到的,设置
window.location
在 iOS 9 上仍然有效。但是,这会弹出一个“在应用程序中打开”对话框。我在 https://bartt.me/openapp 上举了一个例子:
https://lab.bartt.me/openapp 的来源以获取更多信息。
想法: 避免在 Safari 中使用自定义(JavaScript、iframe)解决方案,用受支持的通用链接替换代码。
示例
<html>
<head>
...
</head>
<body>
<div class"app-banner-style">
<a href="http://yourdomain.com">In app open</a>
</div>
...content
</body>
</html>
如果您的应用程序支持通用链接(例如 yourdomain.com),您必须配置您的域(和路径),iOS9 应该对其打开您的应用程序的链接做出反应。
这只是理论,但我想应该是可行的:)
https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12
$('#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://...
});
建议通常是正确的,但有一个主要问题:即使成功打开应用程序,应用程序商店仍然会打开。
解决此问题的方法是通过监视窗口blur
事件来检测第一次弹出窗口何时发生。如果发生模糊,则不要执行回退,因为用户已成功打开应用程序。但如果应用程序处于焦点状态,则执行应用程序商店回退。
var isBlurred = false;
var didFallback = false;
window.addEventListener('blur', function() {
isBlurred = true;
});
window.addEventListener('focus', function() {
isBlurred = false;
});
function appStoreFallback() {
if (isBlurred || didFallback) return;
window.top.location = "https://apps.apple.com/us/app/wavelength/idYOUR_APP_ID";
didFallback = true;
}
window.top.location = 'your-uri-scheme://';
setTimeout(function() {
appStoreFallback();
}, 500);
顺便说一句,这基本上就是 Branch.io 在幕后所做的事情并想要分享。我已经使用它们几年了,但现在停止了,因为它们的 URL 被垃圾邮件过滤器标记,并且如果您的应用程序有适度的使用量,它们的成本会很高。