Defining the Custom URL Scheme (in xCode 4.2)
1. Go to you app info.plist. Add a new row and choose "URL types" as the key (if it doesn't already have this key - by default, it is not there unless you are editing someone else app which already have a URL Scheme defined).
2. Expand 'Item 0' and add in 'URL identifier' from the list.
3. Provide a value for this URL identifier. It can be anything. Although the convention is to use the reverse domain name style, e.g. com.absoluteripple.appname. (I am unclear as to the significance of this identifier as the URL scheme can be used to launch the app with or without this identifier as far as I can tell from my limited testing.)
4. Add another row, and choose 'URLSchemes' from the list. Now, the important bit. Provide the name that will become your custom URL scheme. If you put down 'myappname', then the url scheme will be 'myappname://'.
5. That is it.
Triggering the Launch of your App
Now that you have defined a custom URL scheme, you can use it to launch your app. This is just like any other url and you should (as far as I know) be able to use it like any other url. i.e. instead of 'http://', you use 'myappname://' (or whatever your scheme is). Whereas http:// opens a webpage, myappname:// opens your app. Try it out. e.g. if you type 'myappname://' in the mobile Safari it will open you app (after you have complied your app and put it on the iPhone/ iPad first of course).
Even More Useful: It can be Used to Send Instructions to your App
Just launching the app is good but not that interesting. You can actually use it to pass variables to your app. How you use those variables is entirely up to you and your imagination. The point is that you can use this to get the app to do certain things. e.g. going to a specific location in your app and display certain information. And all this from an external url link.
The magic happens because of the following method (in the UIApplicationDelegate Protocol):
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
This method gets called by iOS whenever the app is launched in response to your custom URL scheme.
A particular interesting variable is the 'url'. This is the url that you use to launch the app. As said above, this is just like any url, so it can have component paths. e.g. myappname://your/path/12345
So by structuring the component paths appropriate based on the needs of your app, you can send your app certain instructions and so get it to do things from an external url.
Notice also, you can tell which application is initiating the launch of your app with the values in 'sourceApplication' which is the bundle ID of the requesting app. You can also use the 'annotation' to to pass in information. This a property-list object. This opens the way for another app to communicate with your app.
Note: For iOS4.2 or before you need to use:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
(this is now depreciated)
Is the App you wish to launch installed?
From another app you can use [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"YOUR_CUSTOM_URL_SCHEME"]] to determine whether the app you wish to launch is installed. This results a bool value.
So the code would look like:
BOOL isAppInstalled = [[UIApplication sharedApplication]
canOpenURL: [NSURL URLWithString:@"YOUR_CUSTOM_URL_SCHEME"]];
if(isAppInstalled) { // launch the app }
else { // do nothing or send user to app store to download the app }
Is it possible to tell whether an app is installed from a browser? Don't know how or even if this is possible.
Useful Reference: