javascript - iOS9:如果可能,尝试通过方案打开应用程序,否则重定向到应用程序商店

标签 javascript ios iframe safari ios9

我的问题是关于 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 技巧不再有效——我猜 Apple 知道它会鼓励更多开发人员更快地实现通用链接。

您仍然可以设置 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 肯定更受限制。

要了解通用链接需要什么的有用概述,请查看我的回答 hereread this tutorial

关于javascript - iOS9:如果可能,尝试通过方案打开应用程序,否则重定向到应用程序商店,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689483/

相关文章:

javascript - 获取地址位置的几何图形

ios - SQLITE更新表问题

javascript - 即使光标没有悬停在 iframe 上,有什么方法可以滚动 iframe 吗?

javascript - 关于fancybox的iframe?

javascript - 如何通过在查询中使用值来选中/取消选中复选框

javascript - 将循环中的计数器分配给变量不会保留其当前值

javascript - Electron ,contextIsolation和contextBridge

iOS 游戏中心——频繁出现连接中断消息

ios - 将 UIView 转换到 SKView 时线程 1 信号 SIGABRT 崩溃

javascript:监听来自特定 iframe 的 postMessage 事件