iOS9: Try to open app via scheme if possible, or redirect to app store otherwise

32,749

Solution 1

The iframe trick no longer works -- my guess is that Apple knows it will encourage more developers to implement Universal Links, more quickly.

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://'; // 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.

For a helpful overview of what's needed for Universal Links should you go that route, check out my answer here or read this tutorial

Solution 2

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/openapp that:

  1. Launches Twitter when the Open in Twitter app is clicked.
  2. Falls back to the Twitter app in the App Store.
  3. Redirects to Twitter or the App Store without the user selecting Open in the Open in App dialog.
  4. Works in all browsers on iOS and Android.

Look at the source of https://lab.bartt.me/openapp for more information.

Solution 3

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.

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 :)

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

Share:
32,749
gran33
Author by

gran33

I am a Software Engineer!

Updated on September 12, 2020

Comments

  • gran33
    gran33 over 3 years

    My question is about iOS9 only!

    I have an HTML landing page, and I try to redirect the user to my app via URL scheme if the app is installed, or redirect to the Appstore otherwise.

    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.

    Any idea why?

    My iframe trick based on this answer.

  • Luigi Saggese
    Luigi Saggese over 8 years
    @carlos.baeza that is a good way to approach to problem, but question refer to javascript, iframe, Safari
  • Tom Roggero
    Tom Roggero over 8 years
    This should be the correct answer @carlos.baeza ... the only problem is the universal links binding is way too complex. But you were right. I up-scored you.
  • st.derrick
    st.derrick over 8 years
    @formatjam yes, if the app is not installed. It's unfortunate.
  • obiuquido144
    obiuquido144 over 8 years
    When the location change and the fallback timer are invoked from a user interaction event (e.g. an onclick handler on a button), as opposed to running e.g. onload, the location change in the timer always executes and the user always ends up being bumped to the app store. It looks like in this interactive scenario, the "Open this page in 'appname'" popup dialog that gets shown as a result of the first window.location assignment doesn't prevent further location changes from happening. Also, it appears the timeout can be as low as 1ms, I didn't see any diff between 500 and 1, Chrome included.
  • obiuquido144
    obiuquido144 over 8 years
    Further to my comment above, to ensure the flow works as expected even in the interactive scenario, you will need to 'create indirection' by wrapping the entire thing (i.e. incl. the first window.location assignment) in another timeout. E.g.: setTimeout(function () {window.location = 'your-uri-scheme://'; setTimeout(function () { window.location = 'itunes.apple.com/us/app/myapp/id123456789?ls=1&mt=8' }, 1); }, 1);
  • Bart Teeuwisse
    Bart Teeuwisse over 8 years
    It does again, was temporarily turned off.
  • Michael
    Michael over 8 years
    @obiuquido144 I don't see how wrapping it in another timeout help. Im my case it didn't.
  • tommybond
    tommybond over 8 years
    This doesn't seem to be working for me - it will show the "Open in app myapp?" dialog for a second and then it will redirect to my fallback url in the setTimeout method. Any idea as to why?
  • tommybond
    tommybond over 8 years
    Has anyone figured this out?
  • jamix
    jamix about 8 years
    @tommy.bonderenka Looks like a 9.2 thing where the "Open this page" dialog is no longer modal, so the redirect is not prevented.
  • tul
    tul over 7 years
    Re point 3, I tested in iOS 9.3.5, and it doesn't appear to be able to open Twitter without the user clicking open in the "Open" button in the "Open this page in 'Twitter'" dialog.
  • Noitidart
    Noitidart almost 7 years
    +1 Does it work in iOS 9 without the "Open in app" dialog? Thanks for sharing. May you please add your code to the post to be in compliance with Stack Overflow rules.
  • glutengo
    glutengo over 6 years
    @BartTeeuwisse can you please provide the code? The linked page results in a 404. Thanks!
  • Bart Teeuwisse
    Bart Teeuwisse over 6 years
    @der_gluateng I've updated the link to the source code.
  • Peter Lapisu
    Peter Lapisu over 6 years
    but it does not open appstore if the app is not installed
  • enigma
    enigma almost 5 years
    on iOS versions >12.3, after opening the App from Safari it redirects to App Store in a split second.
  • Thermometer
    Thermometer over 3 years
    This doesn't seem to work in the iOS Safari browser (anymore?). When user doesn't have Twitter installed on iOS 13 and taps the button, you get the generic 'Safari cannot open this page because it's invalid' alert.