Can I make a link that will open in my app if it's installed, and fall back to a different URL if not?

穿精又带淫゛_ 提交于 2021-02-05 20:31:13

问题


Users can share files from our app via twitter. The tweet includes a URL that points at our server, which detects whether the user is on a mobile device and redirects to a URL using our app's custom scheme so that the link opens in our app.

This works fine for desktop users, and for mobile users who have our app installed; but it doesn't work for mobile users who don't. So what we'd like to do instead is to show all users a page that contains a link which, when pressed, will open the app with the custom URL scheme if it is supported, and open a different URL where the user can download our app if not.

So what I'm looking for is an answer in HTML or JS that looks a bit like this:

<a href="ourapp://www.ourdomain.com/files/12345"
   fallbackhref="http://www.ourdomain.com/buyourapp">Click to download</a>

Is that possible? If so, how do we do it?


回答1:


You can achieve this in Android using the following piece of code :

function openLink () {
    var appWindow = window.open("ourapp://www.ourdomain.com/files/12345","_blank");
    setTimeout( function () {if (appWindow) {
        appWindow.location ="http://www.ourdomain.com/buyourapp";
            }
            },1000);
}

Call the openLink() function on click of a link (else the browser will block the new window as popup).

iOS would differ because of the way it handles custom schemes.

For iOS you need to do the following : Create 2 HTML files with the following code snippets

File #1 : This is your link to open the app/ fallback to website

<script type="text/javascript">
function openLink (url,customURI) {
    window.open("file2.html?lp="+url+"&customURI="+customURI,"_blank");
}
</script>
<img src="IMAGE SOURCE" onclick="openLink('LANDING PAGE','CUSTOM URI')">

File #2 :

<html>
<script>
function openApp () {
    var start, end, elapsed;
    var params =   window.location.href.split("lp=")[1];
    var url     =   params.split("&customURI=")[0];
    var customURI     =   params.split("&customURI=")[1];
    var time = (new Date()).getTime();
    document.location       =       customURI+url;        
    setTimeout(function(){
            var now = (new Date()).getTime();
            if((now - time)<2550) {
            javascript: console.log(new Date().getTime());
                            document.location = url;
            }
            else{
            window.open('', '_self', '');
                    window.close();
        }
    }, 2000);

}
</script>
<body onload="openApp()">
    <a onclick="openApp()" id="newButton">Click Here to open the App</a>
</body>

Hope this helps :)



来源:https://stackoverflow.com/questions/11710902/can-i-make-a-link-that-will-open-in-my-app-if-its-installed-and-fall-back-to-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!