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

前端 未结 3 972
遇见更好的自我
遇见更好的自我 2020-12-13 11:13

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.

相关标签:
3条回答
  • 2020-12-13 11:21

    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>
    
    0 讨论(0)
  • 2020-12-13 11:24

    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>
    
    0 讨论(0)
  • 2020-12-13 11:40

    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.

    0 讨论(0)
提交回复
热议问题