how to prevent iOS safari alert when trying to open non-installed native app?

后端 未结 6 1907
你的背包
你的背包 2020-12-04 09:11

I\'ve been looking for a way to open a native iOS app from the browser. I found a decent solution here: Is it possible to register a http+domain-based URL Scheme for iPhone

相关标签:
6条回答
  • 2020-12-04 09:23

    The right way to do this is to use Smart App Banners: https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

    If your app is not installed, the banner will let the user install it. If it is installed, it will open the app, and you have a way of specifying custom URL arguments to pass to the app.

    0 讨论(0)
  • 2020-12-04 09:27

    Here is a solution that works for me:

    var timeout;
    
    function preventPopup() {
        clearTimeout(timeout);
        timeout = null;
        window.removeEventListener('pagehide', preventPopup);
    }
    
    function openApp() {    
        $('<iframe />')
        .attr('src', appurl)
        .attr('style', 'display:none;')
        .appendTo('body');
    
        timeout = setTimeout(function() {
                document.location = appstore;
        }, 500);
        window.addEventListener('pagehide', preventPopup);
    } 
    
    0 讨论(0)
  • 2020-12-04 09:27

    For solving this and avoid no wanted iOS safari alert I've used a different approach handle also an iframe but without jquery and listener events. It works perfectly.

        var iframe = document.createElement("iframe");
    
        iframe.style.border = "none";
        iframe.style.width = "1px";
        iframe.style.height = "1px";
        iframe.onload = function () {
            document.location = alt;
        };
        iframe.src = nativeSchemaUrl; //iOS app schema url
    
        window.onload = function(){
            document.body.appendChild(iframe);
        }
    
        setTimeout(function(){
            window.location = fallbackUrl; //fallback url
        },300);
    
    0 讨论(0)
  • 2020-12-04 09:31

    use iframe.

    $('<iframe />').attr('src', "appname://").attr('style', 'display:none;').appendTo('body');
    
    0 讨论(0)
  • 2020-12-04 09:35

    I'm using meta refresh as fallback because this works without javascript. This code works in iOS and Android.

    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="refresh" content="1; url=http://www.facebook.com">
        <title>Redirecting..</title>
        <script>
        function tryOpenApp() {
                    var iframe = document.createElement("iframe");
                    iframe.style.border = "none";
                    iframe.style.width = "1px";
                    iframe.style.height = "1px";
                    iframe.src = "fb://feed/";
                    document.body.appendChild(iframe);
        }
        </script>
      </head>
      <body onload="tryOpenApp()">
      </body>
    </html>
    

    Test http://static-pmoretti.herokuapp.com/deep-link/

    0 讨论(0)
  • 2020-12-04 09:36

    For me this does the job:

    window.open(appLink, '_system')`
    
    window.open(appstoreLink, '_system')
    

    Works for android and ios. If the first one can't be opened the second one is called without alert or something. Otherwise it just opens the app.

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