Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Created September 20, 2013 09:38
Show Gist options
  • Save FokkeZB/6635236 to your computer and use it in GitHub Desktop.
Save FokkeZB/6635236 to your computer and use it in GitHub Desktop.
URL schemes for iOS and Android (2/2)
<?
// Settings
$scheme = 'myapp';
$ios_id = 1234567;
$android_package = 'my.app.id';
$auto = false;
// No trailing slash after path, conform to http://x-callback-url.com/specifications/
$REQUEST_URI = preg_replace('@/(?:\?|$)@', '', $_SERVER['REQUEST_URI']);
// Detection
$HTTP_USER_AGENT = strtolower($_SERVER['HTTP_USER_AGENT']);
$android = (bool) strpos($HTTP_USER_AGENT, 'android');
$iphone = !$android && ((bool) strpos($HTTP_USER_AGENT, 'iphone') || (bool) strpos($HTTP_USER_AGENT, 'ipod'));
$ipad = !$android && !$iphone && (bool) strpos($HTTP_USER_AGENT, 'ipad');
$ios = $iphone || $ipad;
$mobile = $android || $ios;
// Install
$ios_install = 'http://itunes.apple.com/app/id' . $ios_id;
$android_install = 'http://play.google.com/store/apps/details?id=' . $android_package;
// Open
if ($ios) {
$open = $scheme . ':/' . $REQUEST_URI;
}
if ($android) {
$open = 'intent:/' . $REQUEST_URI . '#Intent;package=' . $android_package . ';scheme=' . $scheme . ';launchFlags=268435456;end;';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>URL Schemes</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<? if ($ios): ?>
<meta name="apple-itunes-app" content="app-id=<?= $ios_id ?>, app-argument=<?= $open ?>"/>
<? endif ?>
</head>
<body>
<script>
function open() {
window.location = '<?= $open ?>';
<? if ($ios): ?>
setTimeout(function() {
if (!document.webkitHidden) {
window.location = '<?= $ios_install ?>';
}
}, 25);
<? endif ?>
}
</script>
<? if ($mobile): ?>
<? if ($ios): ?>
<p>Click the banner on top of this screen to <a href="<?= $ios_install ?>">install</a> our app or directly <a href="<?= $open ?>">open</a> this content in our app if you have it installed already.</p>
<? elseif ($android): ?>
<p>Go ahead and <a href="<?= $android_install ?>">install</a> our app or directly <a href="<?= $open ?>">open</a> this content in our app if you have it installed already.<p>
<? endif ?>
<? if ($auto): ?>
<script>open();</script>
<? endif ?>
<? else: ?>
<p>Go to the <a href="<?= $ios_install ?>">App Store</a> or <a href="<?= $android_install ?>">Google Play</a> to install and open this content in our app.</p>
<? endif ?>
</body>
</html>
var IS_IPAD = navigator.userAgent.match(/iPad/i) != null,
IS_IPHONE = !IS_IPAD && ((navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null)),
IS_IOS = IS_IPAD || IS_IPHONE,
IS_ANDROID = !IS_IOS && navigator.userAgent.match(/android/i) != null,
IS_MOBILE = IS_IOS || IS_ANDROID;
function open() {
// If it's not an universal app, use IS_IPAD or IS_IPHONE
if (IS_IOS) {
window.location = "myapp://view?id=123";
setTimeout(function() {
// If the user is still here, open the App Store
if (!document.webkitHidden) {
// Replace the Apple ID following '/id'
window.location = 'http://itunes.apple.com/app/id1234567';
}
}, 25);
} else if (IS_ANDROID) {
// Instead of using the actual URL scheme, use 'intent://' for better UX
window.location = 'intent://view?id=123#Intent;package=my.app.id;scheme=myapp;launchFlags=268435456;end;';
}
}
<meta name="apple-itunes-app" content="app-id=1234567, app-argument=myapp://view?id=123"/>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="myapp.com" android:path="/view" />
</intent-filter>
@batlounis
Copy link

This doesn't seem to work on Twitter for iPhone. Any ideas?

@xunuo
Copy link

xunuo commented Jul 21, 2014

good job!

@MantasR
Copy link

MantasR commented Oct 7, 2014

Thanks for a tutorial!

However I updated the open() function. Now it doesn't give an error if it fails to open the app!

function open() {
            <? if ($ios): ?>
            var iFrame = document.createElement('iframe');
            iFrame.style.display = "none";
            iFrame.src = '<?= $open ?>';

            document.body.appendChild(iFrame);
            <? else: ?>
            window.location = '<?= $open ?>';
            <? endif ?>
            /*
            window.location = '<?= $open ?>';
            */
            <? if ($ios): ?>
                setTimeout(function() {
                    if (!document.webkitHidden) {
                        window.location = '<?= $ios_install ?>';
                    }
                }, 5);
            <? endif ?>
        }

The trick is to open the schema in iframe. However, this (iframe trick)doesn't work on Android.

@appscaptain
Copy link

Hi,

Being a non-programmer but I’m trying to open a Google maps location in Google Maps. I want to make sure the user download Google Maps first and then afterwards redirect to the URL in Google Maps. However I can’t make it work. I am loading all-in-one.php and have set the settings like this with the correct id’s for Google Maps on the stores:

// Settings
$scheme = 'comgooglemaps';
$ios_id = 585027354;
$android_package = 'com.google.android.apps.maps';
$auto = true;

But I don’t know how to add the URL Scheme parameters in the script:
comgooglemaps://?q=Cosmo+Coiffure&center=56.157666,10.202283&zoom=14&views=traffic

Tried to place it here:

// No trailing slash after path, conform to http://x-callback-url.com/specifications/
$REQUEST_URI = preg_replace('@/(?:?|$)@', '', $_SERVER['?q=Cosmo+Coiffure&center=56.157666,10.202283&zoom=14&views=traffic']);

But nothing works. Any ideas on how I can do that?

Also, I assume I should only use the "all-in-one.php” script and not the other scripts?

All experts out there who understand how this works?

@appscaptain
Copy link

I spent hours on this yesterday, but it's funny how getting away from it for a while reveals new insights. For anything wondering about the same as me, naturally the open.js and smart.html should be edited, which is actually obvious from above. However modifying open.js and smart.html still does not open the URL Scheme in maps. I think I have replaced everything that needs replacing.

Anyone who knows how to fix this?

These are the files:

open.js

function open() {

    // If it's not an universal app, use IS_IPAD or IS_IPHONE
    if (IS_IOS) {
        window.location = "comgooglemaps://?q=Cosmo+Coiffure&center=56.157666,10.202283&zoom=14&views=traffic";

        setTimeout(function() {

            // If the user is still here, open the App Store
            if (!document.webkitHidden) {

                // Replace the Apple ID following '/id'
                window.location = 'http://itunes.apple.com/app/585027354';
            }
        }, 25);

    } else if (IS_ANDROID) {

        // Instead of using the actual URL scheme, use 'intent://' for better UX
        window.location = 'intent://?q=Cosmo+Coiffure&center=56.157666,10.202283&zoom=14&views=traffic#Intent;package=com.google.android.apps.maps;scheme=comgooglemaps;launchFlags=268435456;end;';
    }
}

all-in-one.php

<?

// Settings
$scheme = 'comgooglemaps';
$ios_id = 585027354;
$android_package = 'com.google.android.apps.maps';
$auto = true;

// No trailing slash after path, conform to http://x-callback-url.com/specifications/
$REQUEST_URI = preg_replace('@/(?:\?|$)@', '', $_SERVER['REQUEST_URI']);

// Detection
$HTTP_USER_AGENT = strtolower($_SERVER['HTTP_USER_AGENT']);
$android = (bool) strpos($HTTP_USER_AGENT, 'android');
$iphone = !$android && ((bool) strpos($HTTP_USER_AGENT, 'iphone') || (bool) strpos($HTTP_USER_AGENT, 'ipod'));
$ipad = !$android && !$iphone && (bool) strpos($HTTP_USER_AGENT, 'ipad');
$ios = $iphone || $ipad;
$mobile = $android || $ios;

// Install
$ios_install = 'http://itunes.apple.com/app/id' . $ios_id;
$android_install = 'http://play.google.com/store/apps/details?id=' . $android_package;

// Open
if ($ios) {
    $open = $scheme . ':/' . $REQUEST_URI;
}
if ($android) {
    $open = 'intent:/' . $REQUEST_URI . '#Intent;package=' . $android_package . ';scheme=' . $scheme . ';launchFlags=268435456;end;';
}

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>URL Schemes</title>
        <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <? if ($ios): ?>
            <meta name="apple-itunes-app" content="app-id=<?= $ios_id ?>, app-argument=<?= $open ?>"/>
        <? endif ?>
    </head>
    <body>

        <script>
        function open() {
            window.location = '<?= $open ?>';

            <? if ($ios): ?>
                setTimeout(function() {
                    if (!document.webkitHidden) {
                        window.location = '<?= $ios_install ?>';
                    }
                }, 25);
            <? endif ?>
        }
        </script>

        <? if ($mobile): ?>

            <? if ($ios): ?>
                <p>Klik p&aring; banneret i toppen af skærmen for at <a href="<?= $ios_install ?>">installere</a> Google Maps eller direkte <a href="<?= $open ?>">&aring;bne</a> anmeldelsessiden i Google Maps hvis du allerede har den installeret.</p>

            <? elseif ($android): ?>
                <p><a href="<?= $android_install ?>">Installer</a> Google Maps eller <a href="<?= $open ?>">åben anmeldelsessiden</a> direkte i Google Maps hvis du allerede har den installeret.<p>
            <? endif ?>

            <? if ($auto): ?>
                <script>open();</script>
            <? endif ?>

        <? else: ?>
            <p>Gå til <a href="<?= $ios_install ?>">App Store</a> eller <a href="<?= $android_install ?>">Google Play</a> for at installere og åbne anmeldelsessiden i Google Maps.</p>
        <? endif ?>

    </body>
</html>

smart.html:

<meta name="apple-itunes-app" content="app-id=585027354, app-argument=comgooglemaps://?q=Cosmo+Coiffure&center=56.157666,10.202283&zoom=14&views=traffic"/>

@Ricardo1980
Copy link

My server is written in Parse (javascript). So, no PHP.
Can I do that using only javascript?

@jerodfritz
Copy link

Anyone aware of any node or javascript only solutions available?

@ajinkyab
Copy link

I want solution for same using Node.js

@abdielou
Copy link

@FokkeZB Using the Intent Anchor on Android will not work correctly if the App is already running in the background. The app will surely open, but alloy.js (Titanium) will not run when the app is only resumed.

@EdvardGilz
Copy link

EdvardGilz commented Jun 9, 2017

This is the js only version:

<script>
// DETECTA EL TIPO DE DISPOSITIVO (USER AGENT)
var IS_IPAD = navigator.userAgent.match(/iPad/i) != null,
    IS_IPHONE = !IS_IPAD && ((navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null)),
    IS_IOS = IS_IPAD || IS_IPHONE,
    IS_ANDROID = !IS_IOS && navigator.userAgent.match(/android/i) != null,
	IS_MOBILE = IS_IOS || IS_ANDROID;
	console.log("IPAD " + IS_IPAD);
	console.log("IPHONE " + IS_IPHONE);
	console.log("IOS " + IS_IOS);
	console.log("ANDROID " + IS_ANDROID);
	console.log("MOBILE " + IS_MOBILE);
this.open();
function open() {
	console.log("open");
    // If it's not an universal app, use IS_IPAD or IS_IPHONE
    if (IS_IOS) {
        window.location = "myapp://view?id=123";
        setTimeout(function() {
            // If the user is still here, open the App Store
            if (!document.webkitHidden) {
                // Replace the Apple ID following '/id'
                window.location = 'http://itunes.apple.com/app/id1234567';
            }
        }, 25);
    } else if (IS_ANDROID) {
        // Instead of using the actual URL scheme, use 'intent://' for better UX
       // I USED THE SAME
        window.location = 'myapp://view?id=123';
    }
}
</script>

But the problem that I have is that Chrome is blocking the navigation.
I've tested with Android so it works with the native Internet browser but not with Chrome.
Anyone knows how to allow Chrome to navigate?

@FokkeZB
Copy link
Author

FokkeZB commented Oct 30, 2017

All, I wasn't receiving notifications for these comments for a while, but opening a Titanium app via an URL has been buggy, in particular on Android. See https://jira.appcelerator.org/browse/TIMOB-20490

@scottandreas
Copy link

Hello

Being a non programmer and going through the code I have a question I have been developing. with appcelerator. . In the app I have a webview that pulls in a remote dynamic webpage URI. They page has many https:// links in it. I would like to have the user open the default device web browser from a click on a URI hyperlink. Will using the URL schema work in my case. Also any help would be greatly appreciated to get me going into implementation. Thank you

@FokkeZB
Copy link
Author

FokkeZB commented Feb 21, 2019

Hi @scottandreas,

I haven't done any Titanium development for the last 2+ years, but I think to break out of the webview, there's an event you can listen to that will fire when the user navigates to a new URL. You'd then be able to open that in the device web browser instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment