我是否可以创建一个在我的应用程序中打开的链接(如果已安装),如果没有,则会回退到其他URL?

Sim*_*mon 5 html javascript url-scheme ios

用户可以通过Twitter从我们的应用程序共享文件.该推文包含一个指向我们服务器的URL,该URL检测用户是否在移动设备上,并使用我们的应用程序的自定义方案重定向到URL,以便在我们的应用程序中打开该链接.

这适用于桌面用户以及安装了我们的应用的移动用户; 但它不适用于没有的移动用户.因此,我们想要做的,而不是什么是显示所有用户,其中包含一个链接,按下时,如果支持将打开自定义URL方案的应用页面,并打开一个不同的URL,用户可以下载我们的如果不是应用程序

所以我正在寻找的是HTML或JS中的答案,看起来有点像这样:

<a href="ourapp://www.ourdomain.com/files/12345"
   fallbackhref="http://www.ourdomain.com/buyourapp">Click to download</a>
Run Code Online (Sandbox Code Playgroud)

那可能吗?如果是这样,我们该怎么做?

Aks*_*hal 5

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);
}
Run Code Online (Sandbox Code Playgroud)

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.

对于 iOS,您需要执行以下操作:使用以下代码片段创建 2 个 HTML 文件

文件 #1:这是您打开应用程序/回退到网站的链接

<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')">
Run Code Online (Sandbox Code Playgroud)

文件#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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)