Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 458
Schlaus Avatar asked May 02 '15 10:05

Schlaus


People also ask

How do I create a redirect URI?

To assign a Redirect URI Policy to a client, go to Token Service -> Clients and edit a chosen client, then go to the Client Application Settings section. Choose a policy from the dropdown menu for the Redirect URI Validation Policy option.

How does a redirect URI work?

A redirect URI, or reply URL, is the location where the authorization server sends the user once the app has been successfully authorized and granted an authorization code or access token.

What is custom URI scheme?

Custom URL schemes provide a way to reference resources inside your app. Users tapping a custom URL in an email, for example, launch your app in a specified context. Other apps can also trigger your app to launch with specific context data; for example, a photo library app might display a specified image.


1 Answers

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.

like image 73
Schlaus Avatar answered Sep 28 '22 05:09

Schlaus