Callmeed
Callmeed

Reputation: 4992

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

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?

Upvotes: 15

Views: 25065

Answers (3)

Loki
Loki

Reputation: 51

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>

Upvotes: 5

padi
padi

Reputation: 797

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>

Upvotes: 16

Perception
Perception

Reputation: 80603

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.

Upvotes: 4

Related Questions