如何重定向到自定义URI方案,或者如果不支持则显示一些内容? [英] How to redirect to custom URI scheme, or show some content if not supported?

查看:119
本文介绍了如何重定向到自定义URI方案,或者如果不支持则显示一些内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:是否可以将访问者重定向到自定义URI方案,或者如果不支持该方案,则显示一些内容?

To put it short: Is it possible to redirect a visitor to a custom URI scheme, or show some content if that scheme is not supported?

我的特殊用例是我正在创建一个注册自定义URI方案的移动应用程序,以便用户可以通过短信或电子邮件发送链接,邀请其他用户访问应用程序中的某些操作。

My particular use case is that I'm creating a mobile app that registers a custom URI scheme so that users can invite other users to certain actions within the app by sending links via SMS or e-mail.

链接指向我的服务器(在Apache上运行PHP),服务器将访问者重定向到正确的方案。只要这是所有重定向页面都能正常工作,但我希望能够显示一些内容,以防在计算机或其他没有安装我的应用程序的设备上打开电子邮件。

The links point to my server (running PHP on Apache), and the server redirects the visitors to the proper scheme. This works perfectly as long as that's all the redirect page does, but I'd like to be able to show some content in case the e-mail is opened on a computer or some other device that doesn't have my app installed.

我试图通过这些Javascript技巧以及服务器上的PHP脚本提供Location头和内容。两者都不起作用。我还尝试在页面上使用< meta http-equiv =Locationcontent =myscheme:// testing> 标记,但这没有做到任何事情。

I've tried to achieve this with these Javascript tricks as well as serving both a Location header and the content from the PHP script on the server. Neither works. I also tried using a <meta http-equiv="Location" content="myscheme://testing"> tag on the page, but that didn't do anything either.

有些人建议使用用户代理嗅探来查看客户端是使用移动设备还是桌面浏览器。我已经在进行此项操作以及初步检查,但仍然可以在没有安装我的应用程序的移动设备上打开链接,并且这些人将留下空白页面。

Some people have suggested using user-agent sniffing to see whether the client is using a mobile or a desktop browser. I am already doing this as well as a preliminary check, but it still leaves the possibility the link is opened on a mobile device that doesn't have my app installed, and those people would be left with an empty page.

有没有办法实现这一点,还是我运气不好?

Is there some way to achieve this, or am I out of luck?

推荐答案

修订后的版本,原文在底部:

为了保持快速和干净,我决定保留重定向页面。此外,我认为重定向页面不应该保留在浏览器的历史记录中,以避免永无止境的后退按钮惨败。因此我最终得到了这个版本:

To keep things quick and clean, I decided to keep the redirect page as just that. Furthermore I figured that the redirect page shouldn't stay in the browser's history to avoid never-ending back-button fiascos. Thus I ended up with this version:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Redirecting...</title>
    <script>
        var redirectToApp = function() {
            setTimeout(function appNotInstalled() {
                window.location.replace("http://example.com/app-not-installed");
            }, 100);
            window.location.replace("myscheme:someaction");
        };
        window.onload = redirectToApp;
    </script>
</head>
<body></body>
</html>

原始答案:

经过一些小小的讨论,我发现这实际上可以用Javascript实现。我只是让它比我的更简单:

After some more fiddling I found that this is in fact possible with Javascript. I just had to make it a bit simpler than what I had:

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to my app</title>
    <script>
        var redirectToApp = function() {
            document.location = 'myscheme:someaction';
        };

        window.onload = redirectToApp;
    </script>
</head>

<body>
You don't have the app installed.
</body>
</html>

这正是我所需要的。不幸的是,当重定向无法完成时,它会导致Javascript控制台出错,但我想我只能忍受这种错误。

This does exactly what I need. It does unfortunately cause an error in the Javascript console when the redirect can't be done, but I guess I'll just have to live with that.

这篇关于如何重定向到自定义URI方案,或者如果不支持则显示一些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆