How to redirect from Mobile Safari to Native iOS app (like Quora)?

23,404

Solution 1

I'm reposting an answer to my own related (but was originally Ruby-on-Rails-specific) question from here: Rails: redirect_to 'myapp://' to call iOS app from mobile safari

You can redirect using javascript window.location.

Sample code:

<html><head>
    <script type="text/javascript">
      var userAgent = window.navigator.userAgent;
      if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
        window.location = "myiosapp://"
      }
    </script>
  </head>
  <body>
    Some html page
  </body>
</html>

Solution 2

Just a small improvement of the JS code, if the app is not installed, it will send the user to itunes store ;)

<script type="text/javascript">

    // detect if safari mobile
    function isMobileSafari() {
        return navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/)
    }
    //Launch the element in your app if it's already installed on the phone
    function LaunchApp(){
      window.open("Myapp://TheElementThatIWantToSend","_self");
    };

    if (isMobileSafari()){
        // To avoid the "protocol not supported" alert, fail must open itunes store to dl the app, add a link to your app on the store
        var appstorefail = "https://itunes.apple.com/app/Myapp";
        var loadedAt = +new Date;
        setTimeout(
          function(){
            if (+new Date - loadedAt < 2000){
              window.location = appstorefail;
            }
          }
        ,100);
        LaunchApp()

    }

</script>

Solution 3

You can do trigger your application to be launched using custom URL scheme, registered by your application with the iOS runtime. Then on your website, write code to detect the incoming User-Agent and if iOS is detected generate your custom URL's instead of regular http ones.

Share:
23,404
Callmeed
Author by

Callmeed

Husband, father, hacker, entrepreneur and photographer. Co-founder and CTO at BIG Folio and NextProof Twitter @callmeed

Updated on April 07, 2020

Comments

  • Callmeed
    Callmeed about 4 years

    On my iPhone, I just noticed that if I do a Google Search (in Mobile Safari) and select a result on quora.com, the result page launches the native Quora app on my phone.

    How is this done? Specifically, is it a detection of the user agent and the use of an iOS URL scheme? Can it tell if the native app is installed and/or redirect to the app store?

  • Callmeed
    Callmeed about 12 years
    So, what happens if they don't have the app installed?
  • Perception
    Perception about 12 years
    Good question, because there isn't a standard way of handling this. But here is a decent workaround posted by another user of this site.
  • Jonny
    Jonny over 10 years
    Does not work on my iOS 7 device IF I launch the webapp from a webclip in fullscreen mode. It has to be no fullscreen mode setting for the webclip.
  • kkurni
    kkurni about 10 years
    Where is the workaround for detecting if your app is installed without error message ?
  • Reyraa
    Reyraa about 8 years
    Is this a proper solution? This would force redirect the user to the application every time he/she opens the URL. which it's annoying.