如果安装了我的应用程序,我是否可以创建一个可以在我的应用程序中打开的链接,如果没有,该链接可以退回到另一个URL? [英] Can I make a link that will open in my app if it's installed, and fall back to a different URL if not?
问题描述
用户可以通过Twitter从我们的应用程序共享文件.该推文中包含指向我们服务器的URL,该URL可检测用户是否在移动设备上,并使用我们应用的自定义方案重定向到URL,以便在我们的应用中打开链接.
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.
这对于台式机用户和安装了我们应用程序的移动用户而言效果很好;但不适用于不这样做的移动用户.因此,我们要做的是向所有用户显示一个包含链接的页面,当按下该页面时,将使用自定义URL方案打开应用程序(如果受支持),并打开一个不同的URL,供用户下载我们的网址.应用程序.
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.
所以我要寻找的是HTML或JS的答案,看起来像这样:
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?
推荐答案
您可以使用以下代码在Android中实现此目标:
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);
}
点击链接后调用 openLink()
函数(否则浏览器将阻止新窗口作为弹出窗口.)
Call the openLink()
function on click of a link (else the browser will block the new window as popup).
iOS因处理自定义方案的方式而异.
iOS would differ because of the way it handles custom schemes.
对于iOS,您需要执行以下操作:使用以下代码段创建2个HTML文件
For iOS you need to do the following : Create 2 HTML files with the following code snippets
文件1:这是您打开应用程序/后备网站的链接
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')">
文件#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>
希望这会有所帮助:)
这篇关于如果安装了我的应用程序,我是否可以创建一个可以在我的应用程序中打开的链接,如果没有,该链接可以退回到另一个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!