Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom url scheme to native app or website

Tags:

iphone

I'm wanting to develop a url that if my application is installed, it will be handled through the app. If it is not an iPhone or our app is not installed I want to redirect to a web url.

Basically exactly the same as an app store url work.

like image 601
kgibbon Avatar asked Dec 20 '11 05:12

kgibbon


2 Answers

Unfortunately custom URL handlers on iOS don't work that way.

You can define custom url schemes that will open your app, but you can't make your app the designated handler for certain domain names so that opening that domain in Safari will launch your app automatically.

To be clear, a scheme is the bit before the domain name, like http:, so you could make your app the handler for urls that start myapp: for example. Obviously no real URLs start with myapp: except for ones that you've designed specifically to be used with your app - that's the whole point.

Unfortunately these URLs will only work with your app, they can't be opened in Safari if your app isn't installed. iTunes, Google maps, Youtube, etc all work that way on iPhone because Apple has hard-coded them as special cases, but they don't make this mechanism available to third party apps.

To register a custom scheme for your app, follow this tutorial: http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html

What you may be able to do instead is set up a regular web page which uses javascript to detect the user agent of the device and if it's an iPhone redirect to the app's custom scheme url automatically using document.location = 'myapp:...'. I'm not sure what happens if you try to redirect to a custom url scheme if the app's not installed though. It may do nothing, which would be ideal for you, or it may throw up an error or go to a blank page in which case you'd be better off popping up a message like "click here to launch the app or click here to download it from the app store", which is what most sites seem to do.

like image 130
Nick Lockwood Avatar answered Oct 01 '22 07:10

Nick Lockwood


You have to make a normal Website with normal URL with then redirects to an URL like yourapp://dosomething. Webbrowser, like Safari should ignore the url if there is no App (your app) with handles the protocol "yourapp://".

Redirecting is e.g. possible with setting the header with a redirect in php:

<?php
header('Location: yourapp://dosomething');
?>

It is possible for other Server Script Languages too. You should also include a distinction between "MobileSafari" and other Browsers, and just to it there.

<?php

/* detect Mobile Safari */

$browserAsString = $_SERVER['HTTP_USER_AGENT'];

if (strstr($browserAsString, " AppleWebKit/") && strstr($browserAsString, " Mobile/"))
{
    header('Location: yourapp://dosomething');
}

?>
like image 20
Maffo Avatar answered Oct 01 '22 08:10

Maffo