找到你要的答案

Q:How to redirect to custom URI scheme, or show some content if not supported?

Q:如何重定向到自定义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?

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.

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.

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?

把它短:是可能重定向访问者自定义URI方案,或显示某些内容如果方案不支持?

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

链接指向我的服务器(Apache运行PHP),和服务器重定向访问者到正确的方案。这是完美的,只要这是所有的重定向页做,但我想能够显示一些内容的情况下,电子邮件是打开电脑或其他设备,没有我的应用程序安装。

我试图实现这些JavaScript技巧以及服务定位头和PHP脚本在服务器上的内容。没有工作的。我也试过用<;元http-equiv =“位置”内容=“我的方案:/ /测试”& gt;标签在页面上,但是没有做任何东西。

有些人建议使用用户代理嗅探,看看客户端是否使用移动或桌面浏览器。我已经做了这一点,以及初步检查,但它仍然离开链接的可能性是开放的移动设备,没有我的应用程序安装,这些人将留下一个空页。

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

answer1: 回答1:

Revised version, original at the bottom:

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>

Original answer:

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>

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.

修订版,原文在底部:

为了保持快速和干净的东西,我决定保持重定向页面只是。而且我认为重定向页面不应该呆在浏览器的历史记录来避免无休止的后退按钮的惨败。因此,我结束了这个版本:

<!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。我只需要做得比我所拥有的简单一点:

<!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控制台时导致错误的重定向是不可能的事,但我想我会这样生活。

answer2: 回答2:

In your php script you could check user agent to see if a desktop or mobile browser. In case that would be a desktop browser then you will redirect the user to http application.

"Updated answer": I think that you can't, please see this facebook flow: https://developers.facebook.com/docs/applinks/android

But, I think that you could create your deeplinking using the scheme "http" with your web as: "http://example.com/" so mobile intercept that url as a deep linking and open your application instead of only the browser: https://developer.android.com/training/app-indexing/deep-linking.html

在你的PHP脚本可以检查用户代理看看桌面或移动浏览器。如果将桌面浏览器,那么你将用户重定向到HTTP应用。

"Updated answer": I think that you can't, please see this facebook flow: https://developers.facebook.com/docs/applinks/android

But, I think that you could create your deeplinking using the scheme "http" with your web as: "http://example.com/" so mobile intercept that url as a deep linking and open your application instead of only the browser: https://developer.android.com/training/app-indexing/deep-linking.html

answer3: 回答3:

If you want another, free and much simpler solution that doesn't involve constant tweaking on your server to support different browsers, check out branch.io. The links have all of this code in them, but if you use the SDK, you can even receive the URI path post install (for new users). The links support Facebook AppLinks, Twitter cards and pretty much everything out of the box.

To create links, you can use the dashboard, API or SDKs. In your case, the SDK is most suitable for user to user invites. Here's an example on how to create a link and customize the redirect endpoints:

JSONObject dataToInclude = new JSONObject();

try {
    // customize the display of the Branch link
    dataToInclude.put("$og_title", "Joe's My App Referral");
    dataToInclude.put("$og_image_url", "https://s3-us-west-1.amazonaws.com/myapp/joes_pic.jpg");
    dataToInclude.put("$og_description", "Join Joe in My App - it's awesome");

    // customize the desktop redirect location
    dataToInclude.put("$desktop_url", "http://example.com/app-not-installed");
} catch (JSONException ex) { }

Branch branch = Branch.getInstance();
branch.getShortUrl("text_message", Branch.FEATURE_TAG_SHARE, "share_screen", dataToInclude, new BranchLinkCreateListener() {
    @Override
    public void onLinkCreate(String url, Branch.BranchError error) {
        if (error == null) {
           // show the link to the user or share it immediately
        } else {
           Log.i("MyApp", error.getMessage());
        }
    }
});

如果你想要另一个,自由和更简单的解决方案,不需要不断的调整你的服务器来支持不同的浏览器,看看branch.io。的链接在他们所有的代码,但是如果你使用SDK,你甚至可以接收URI路径安装后(新用户)。链接支持脸谱网AppLinks,推特卡和几乎所有的开箱。

创建的链接,你可以使用仪表板、API或SDK。在您的情况下,SDK是最适合用户的邀请。下面是如何创建链接并自定义重定向端点的示例:

JSONObject dataToInclude = new JSONObject();

try {
    // customize the display of the Branch link
    dataToInclude.put("$og_title", "Joe's My App Referral");
    dataToInclude.put("$og_image_url", "https://s3-us-west-1.amazonaws.com/myapp/joes_pic.jpg");
    dataToInclude.put("$og_description", "Join Joe in My App - it's awesome");

    // customize the desktop redirect location
    dataToInclude.put("$desktop_url", "http://example.com/app-not-installed");
} catch (JSONException ex) { }

Branch branch = Branch.getInstance();
branch.getShortUrl("text_message", Branch.FEATURE_TAG_SHARE, "share_screen", dataToInclude, new BranchLinkCreateListener() {
    @Override
    public void onLinkCreate(String url, Branch.BranchError error) {
        if (error == null) {
           // show the link to the user or share it immediately
        } else {
           Log.i("MyApp", error.getMessage());
        }
    }
});
javascript  php  http  redirect