title: iOS9: Try to open app via scheme if possible, or redirect to app store otherwise
body: <p>My question is about <strong>iOS9 only!</strong></p>
  
  <p>I have an <code>HTML</code> landing page, and I try to redirect the user <strong>to my app</strong> via URL scheme if the app is installed, or redirect to the <strong>Appstore</strong> otherwise.</p>
  
  <p>My code is:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>The problem is that the <code>iframe</code> trick doesn't work in <code>Safari</code> <code>iOS9</code>.</p>
  
  <p>Any idea why?</p>
  
  <p>My <code>iframe</code> trick based on <a href="https://stackoverflow.com/questions/19701708/how-to-prevent-ios-safari-alert-when-trying-to-open-non-installed-native-app/25054876#25054876">this</a> answer.</p>