Wednesday, December 19, 2012

Opening iOS Apps from Browser or any other source using URL Scheme in iOS Devices

In this section, i will be describing about how to open up an app in iOS device either from a Browser or from any other application present in iOS devices.

First thing you need to do is to register a URL scheme  in to device which is done in your app's info.plist file. URL Scheme will be your key to open up your app from browser or any other app. Here are the step by step procedure :
First open up your info.plist
Step 1. Right-Click and “Add Row”
screen-capture.png
Step 2. Select “URL types” as the Key
screen-capture-1.png
Step 3. Expand “Item 1″ and provide a value for the URL identifier. This is your App Bundle Identifier (eg : com.yourcompany.yourAppName).
screen-capture-2.png
Step 4. Add another row, this time add row to “Item 1″.
screen-capture-3.png
Step 5. Select “URL Schemes” as the Key.
screen-capture-4.png
Step 6. Enter the characters that will become your URL scheme (e.g. if you put myapp here then "myapp://" will be your URL Scheme you need to write in browser or any other place, which will invoke your app). It is possible for more than one scheme to be registered by adding to this section though that would be strange thing to do.
screen-capture-6.png

Now that the URL has been registered as "myapp". Anyone can start the application by opening a URL using your scheme.
Here are a few examples that will open up your app…(Try by typing it to device safari browser )
myapp://
myapp://testToGo
myapp://testToGoWithParameter/user=123
The iPhone SDK, when launching the application in response to any of the URLs format as above (starting with myapp://), will send a message to the UIApplicationDelegate. and will trigger the following delegate method :
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{
  // Do something with the url here
}
Now, you can parse your URL in this delegate method and can perform any task you wish to (like pushing to some other Views, generating some alerts where app is coming from, parse the parameter that the URL is linked with for some special purpose etc. That means its the place from where your logic starts, how you want your app to react if app is launching from some other source and browsers, if you don't want to do anything beside simply launching the app then there should be no headache, since your app is already launched !!!)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (!url) {  return NO; }
 
    NSString *URLString = [url absoluteString];
    // Now apply your app flow if you have any!!!
    return YES;
}                                                             
And atlast, remember that not only you can open up your app from browser or from other places, you can even pass information to your app aswell in the URL format, you just have to parse it in above delegate method and remember your URL format should start from URLSchemeName://  (eg: myapp:// refering our tutorial).   HAPPY CODING !!!!      

No comments:

Post a Comment