kgibbon
kgibbon

Reputation: 726

custom url scheme to native app or website

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.

Upvotes: 5

Views: 10390

Answers (3)

Nick Lockwood
Nick Lockwood

Reputation: 40995

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.

Upvotes: 8

Maffo
Maffo

Reputation: 1465

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');
}

?>

Upvotes: 7

Maulik
Maulik

Reputation: 19418

You can use canOpenURL: method to check application can open a given URL resource or not. More on Apple's Doc.

Hope it helps you.

Upvotes: 0

Related Questions