Translate

Tuesday, April 2, 2013

iOS 6 SDK: Displaying App Store Products In-App


Have you ever had the need to send a customer from your iOS application to the App Store? Maybe you wanted her to rate your application in the App Store or you just wanted to promote one of your other apps. Prior to iOS 6, the only viable option was to send the customer to the App Store application. In iOS 6, however, Apple introduced the SKStoreProductViewController class, which allows an application to show a product in the App Store without leaving the application. In this quick tip, I will show you how this works.

Store Kit

As its class prefix indicates, the SKStoreProductViewController class is a member of the Store Kitframework. Using SKStoreProductViewController is incredibly easy. Before we take a look at an example application, it is useful to understand how everything fits together.
The SKStoreProductViewController class is a subclass of UIViewController, which means that it is easy to use if you are familiar with view controllers. Whenever you want to show a customer a product in the App Store, you (1) instantiate an instance of the SKStoreProductViewController class, (2) set its delegate, and (3) present the store product view controller to the customer. The operating system takes care of the rest. Keep in mind that an instance of the SKStoreProductViewController class can only be presented modally.
The SKStoreProductViewControllerDelegate delegate protocol defines a single method,productViewControllerDidFinish:. This method is called when the customer leaves the App Store, usually by tapping the cancel button in the top left of the view. By sending the delegate the message ofproductViewControllerDidFinish:, the operating system gives control back to your application. Let me show you how to use the SKStoreProductViewController class by creating a sample application.

Setting Up the Project

The application that we are about to build is not very functional as it only has one button, which takes the user to the App Store and shows the user Drizzle, a simple weather application that I released a few weeks ago. However, it shows you how the different pieces fit together and how to use theSKStoreProductViewController class in your projects.
Create a new project in Xcode by selecting the Single View Application template from the list of templates (figure 1). Name your application App Store, enter a company identifier, set iPhone for the device family, and check Use Automatic Reference Counting. The rest of the checkboxes can be left unchecked for this project (figure 2). Tell Xcode where you want to save the project and hit the Create button.
New in iOS 6: SKStoreProductViewController: Choosing a Project Template - Figure 1
New in iOS 6: SKStoreProductViewController: Configuring the New Project - Figure 2

 Adding the Store Kit Framework

Because the SKStoreProductViewController class is part of the Store Kit framework, we need to link our project against the Store Kit framework. Select the project in the Project Navigator and choose the target in the list of targets. At the top, choose the Build Phases tab and open the Link Binary With Libraries drawer. Click the button with the plus sign and choose StoreKit.framework form the list that appears (figure 3). You have now successfully linked your project against the Store Kit framework.
New in iOS 6: SKStoreProductViewController: Linking the Project Against the Store Kit Framework - Figure 3
To use the Store Kit framework in the MTViewController class, we need to import the framework’s header files. Open MTViewController.h and add the following import statement at the top.
1
#import <StoreKit/StoreKit.h>

Using theSKStoreProductViewControllerClass 

In the view controller’s viewDidLoad method, create a new button as shown in the snippet below. The button is of type UIButtonTypeRoundedRect and we position it at the center of the view controller’s view. We also give it a descriptive title and add a target-action pair to the UIControlEventTouchUpInsideevent. This means that whenever the user taps the button, the view controller receives a message ofopenAppStore:. This is the method where the magic happens.
1
2
3
4
5
6
7
8
9
10
11
- (void)viewDidLoad {
    [super viewDidLoad];
    // Initialize Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Go to App Store" forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0.0, 0.0, 200.0, 44.0)];
    [button setCenter:self.view.center];
    [self.view addSubview:button];
    // Add Target-Action Pair
    [button addTarget:self action:@selector(openAppStore:) forControlEvents:UIControlEventTouchUpInside];
}
In the openAppStore: method, we initialize an instance of the SKStoreProductViewController class, set the delegate to self, and send it a message of loadProductWithParameters:completionBlock:. The loadProductWithParameters:completionBlock: accepts two arguments, (1) a dictionary with a key specifying the application identifier of the application that we want to show to the user and (2) a completion block. The completion block is executed when the request to the App Store is finished. In the completion block, we verify if no error was thrown and present the store product view controller to the user. Keep in mind that even though the user doesn’t leave your application, the operating system does connect to the App Store under the hood. It is also important to note that the request to the App Store can take a non-trivial amount of time. In other words, it is good practice to show an activity indicator to the user as long as the request has not returned a response. The completion block will allow us to dismiss the activity indicator once the request has finished, successfully or unsuccessfully.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)openAppStore:(id)sender {
    // Initialize Product View Controller
    SKStoreProductViewController *storeProductViewController = [[SKStoreProductViewController alloc] init];
    // Configure View Controller
    [storeProductViewController setDelegate:self];
    [storeProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier : @"594467299"} completionBlock:^(BOOL result, NSError *error) {
        if (error) {
            NSLog(@"Error %@ with User Info %@.", error, [error userInfo]);
        } else {
            // Present Store Product View Controller
            [self presentViewController:storeProductViewController animated:YES completion:nil];
        }
    }];
}
You can find the unique identifier of an application in iTunes Connect. Every application in the App Store is given a unique identifier or Apple ID. Note that you need to pass the Apple ID in the parameters dictionary as a string.
Before building and running the application, we need to conform the MTViewController class to theSKStoreProductViewControllerDelegate protocol by implementing theproductViewControllerDidFinish: method. We start by updating the view controller’s interface file by telling the compiler that the MTViewController class conforms to theSKStoreProductViewControllerDelegate protocol (see below).
1
2
3
4
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface MTViewController : UIViewController <SKStoreProductViewControllerDelegate>
@end
In the view controller’s implementation file, implement the productViewControllerDidFinish: method as shown below. Remember that the store product view controller is presented modally when we invoke theloadProductWithParameters:completionBlock: method. It is our responsibility to dismiss the store product view controller when the customer decides to leave the App Store.
1
2
3
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Build and Run

Build and run the application to try it out. Even though Apple advertises theSKStoreProductViewController class as a way to show other apps to your users, it is also an ideal way to give users the chance to rate or review an app in the App Store without the hassle of leaving your application.
New in iOS 6: SKStoreProductViewController: The Store Product View Controller in Action - Figure 4


The SKStoreProductViewController class is a welcome addition to the Store Kit framework and I have already taken advantage of this new addition in some of my applications. I hope that I have convinced you of its usefulness with this quick tip.

No comments:

Post a Comment