iOS SDK: Open other app from our app

1. Open iPhone application from our app

NOTE-> TEST in Device.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
@"itms-apps://YourApplicationLink"]];


Example -> 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/whats-your-bmi/id595331418?ls=1&mt=8"]];


2. Submit review for any application 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=YOURAPPID&mt=8"]];


Example -> 





3.Open our own application


You have to create two apps, one app that sends data, and another app that receives that data. Together, these apps will demonstrate URL schemes, a method for sending and receiving data between applications.

***If you don't want to create two app then just chill ;) , Just implement few things. :)

You can download here , but i suggest you to learn this :)

In your Sender application just implement button press event or whatever you want



-(IBAction) openReceiverApp:(id)sender {
    // Opens the Receiver app if installed, otherwise displays an error
    UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *URLEncodedText = [@"Your text here...... :)" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *ourPath = [@"rajneesh071://" stringByAppendingString:URLEncodedText];
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    if ([ourApplication canOpenURL:ourURL]) {
        [ourApplication openURL:ourURL];
    }
    else {
        //Display error
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Receiver Not Found" message:@"The Receiver App is not installed. It must be installed to send text." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
}

These methods are using the openURL method of the UIApplication to send data to other apps. Apple has registered the Maps application and YouTube application with “http://” essentially, so we simply callopenURL on those URLs. To create our URL, we also used thestringByAddingPercentEscapesUsingEncoding: method to ensure the string is a valid URL by URL encoding the string (we will decode it in our Receiver app). For our custom URL “ rajneesh071://” we first check if the link can be opened with canOpenURL. This essentially checks whether the app that is registered to that particular URL scheme is installed, and if it is, we are able to open the URL with our text. If the app is not installed, we display an error message. Remember that when you release your app to the public, the URL scheme your app is dependent on might not work because the other app isn’t installed. You should always perform the canOpenURL when opening non-http:// URL schemes.

Part1

In your Receiver application

Step1 >>>>> Register the Custom URL Scheme
  open the Receiver Application's  Info.plist file.
You can add a new row by going to the menu and clicking Editor > Add Item. Set up a URL Types item by adding a new item. Expand the URL Types key, expand Item 0, and add a new item, “URL schemes”. Fill in “readtext” for Item 0 of “URL schemes” and your company identifier for the “URL Identifier”. Your file should resemble the image below when done.
Step2 >>>>> Handle the URL
Open ReceiverAppDelegate.m and
Implement  application:applicationDidFinishLaunchingWithOptions:method.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    // Display text
    UIAlertView *alertView;
    NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    alertView = [[UIAlertView alloc] initWithTitle:@"Text" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    return YES;
}
This code alerts the user with the text that was sent by the Sender app when the application is opened with a URL. Generally, you should use this data to follow up with an appropriate action within your app. Notice that we used the hostname of the URL to get our text. We did this because the URL scheme we registered functions like any other URL “scheme://hostname/path/file.php?variable=x#section” (Recall that our URL was “rajneesh071://text”). We also URL decoded the text using thestringByReplacingPercentEscapesUsingEncoding: method as previously we had URL encoded it.

Thats it, now first run receiver app and then run sender app :)

Conclusion

If successful, you should now be able to easily implement inter-app communications using URL schemes. If you have any questions or comments, feel free to leave them in the comments section below. Thanks for reading!

Additional Information and Resources

Pros:
  • Does not force the user to be connected to a network, or require additional resources for web server handling.
  • A simple, fast, and easy method of implementing communication.
  • Provide a public communication interface that ANY app can take advantage of.
  • Open your application from your website using an anchor tag. Ex: <a href="readtext://">Open Our iPhone Application</a>
Cons:
  • Unlike other platforms such as Android, iPhone does not create a stack of actions (back stacking on Android). What this means is that if you do decide to launch another application, your application will not resume when the user exits from the application you opened.
When not to use URL Schemes:
  • Sending user-sensitive data such as username and password combinations. You should never do this; instead refer to the Keychain API.
  • Avoid using URL Schemes when you can implement the other applications functionality internally and directly, to avoid having your application close. For example, should you use a URL scheme to launch the Maps application when you could implement Maps in-app? Depending on the situation, you may have to but in many cases opening Maps in-app is sufficient.
  • No authentication that the data you have sent will reach the correct application or whether it has reached at all, namely why sensitive data should not be sent.
Resources:

Comments

  1. Your tutorial is nice. But I found simple thing missing when you try to open your own app (Receiver) from one of your other app (Sender).

    The missing thing is LSApplicationQueriesSchemes key in the Sender-Info.plist

    It should be array and for the first element you have to add "rajneesh071" which is the Receiver's URL schemes first item value.

    That's how custom apps connect with each other.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hacker Rank problem solution

How to Make REST api call in Objective-C.

Building and Running Python Scripts with Xcode