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

gra*_*n33 39 javascript safari iframe ios 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);
}
Run Code Online (Sandbox Code Playgroud)

问题是这个iframe技巧无效Safari iOS9.

知道为什么吗?

我的iframe诀窍基于这个答案.

st.*_*ick 23

iframe技巧不再适用 - 我的猜测是Apple知道它会鼓励更多开发人员更快地实施Universal Links.

您仍然可以window.location='your-uri-scheme://';在500ms后设置和回退到App Store.如果采用这种方法,弹出窗口之间会有"跳舞",就像我们在分支机构那样(如果Universal Links不起作用,我们会作为后备).

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);
Run Code Online (Sandbox Code Playgroud)

我希望我能给你一个更好的答案.iOS 9肯定更受限制.

对于你应该走这条路什么需要通用链接有用的概述,检查出我的答案在这里阅读本教程

  • 这对我来说似乎没有用 - 它会显示"在app app中打开?" 对话框一秒钟,然后它将重定向到setTimeout方法中的我的后备网址.知道为什么? (4认同)
  • @ obiuquido144我没看到如何将它包装在另一个超时帮助中.我的情况不是. (2认同)

car*_*eza 1

也许尝试为您提供对通用链接的应用程序支持

想法:避免在 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>
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序支持通用链接(例如 yourdomain.com),您必须配置您的域(和路径),iOS9 应该对打开您的应用程序的链接做出反应。这只是理论,但我想应该可行:)

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12