Advertisement
iOS SDK

iOS 6 SDK: Displaying App Store Products In-App

by

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 Kit framework. 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 of productViewControllerDidFinish:, the operating system gives control back to your application. Let me show you how to use the SKStoreProductViewController class by creating a sample application.


Step 1: 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 the SKStoreProductViewController 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

Step 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.

#import <StoreKit/StoreKit.h>

Step 3: Using the SKStoreProductViewController Class

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 UIControlEventTouchUpInside event. This means that whenever the user taps the button, the view controller receives a message of openAppStore:. This is the method where the magic happens.

- (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.

- (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 the SKStoreProductViewControllerDelegate protocol by implementing the productViewControllerDidFinish: method. We start by updating the view controller's interface file by telling the compiler that the MTViewController class conforms to the SKStoreProductViewControllerDelegate protocol (see below).

#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 the loadProductWithParameters:completionBlock: method. It is our responsibility to dismiss the store product view controller when the customer decides to leave the App Store.

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Step 4: Build and Run

Build and run the application to try it out. Even though Apple advertises the SKStoreProductViewController 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

Conclusion

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.

Related Posts
  • Code
    iOS SDK
    Exploring the Multipeer Connectivity framework: Game Logic78mpi preview image
    In this tutorial, I will show you how to create a simple, multi-player game using the Multipeer Connectivity framework that was introduced in iOS 7. In the first installment of this series, we laid the foundation of the game. In this article, we'll implement the game logic.Read More…
  • Code
    iOS SDK
    Exploring the Multipeer Connectivity framework: Project Setup78mpi preview image
    As with every major release, iOS 7 includes many new APIs that developers can take advantage of in their applications. In this tutorial, we'll take a look at a brand new framework introduced in iOS 7, the Multipeer Connectivity framework. This framework adds support for discovering, connecting to, and communicating with nearby services, such as iOS devices. In this tutorial, I will show you how to create a simple, multi-player game using this new framework.Read More…
  • Code
    iOS SDK
    Securing and Encrypting Data on iOSPs8e2e preview image@2x
    Whether you're creating a mobile application or a web service, keeping sensitive data secure is important and security has become an essential aspect of every software product. In this tutorial, I will show you how to safely store user credentials using the application's keychain and we'll take a look at encrypting and decrypting user data using a third party library.Read More…
  • Code
    iOS SDK
    Blocks and Table View Cells on iOS94dp7 preview image@2x
    A table view cell doesn't know about the table view it belongs to and that's fine. In fact, that's how it should be. However, people who are new to this concept are often confused by it. For example, if the user taps a button in a table view cell, how do you obtain the index path of the cell so you can fetch the corresponding model? In this tutorial, I'll show you how not to do this, how it's usually done, and how to do this with style and elegance.Read More…
  • Code
    iOS SDK
    Working with NSURLSession: Part 3E548b preview image@2x
    In the previous tutorials, we explored the fundamentals of the NSURLSession API. There is one other feature of the NSURLSession API that we haven't look into yet, that is, out-of-process uploads and downloads. In the next two tutorials, I will show you how to create a very simple podcast client that enables background downloads.Read More…
  • Code
    iOS SDK
    Networking with NSURLSession: Part 2E548b preview image@2x
    In the previous tutorial, I introduced you to NSURLSession. I talked about the advantages it has over NSURLConnection and how to use NSURLSession for simple tasks, such as fetching data from a web service and downloading an image from the web. In this tutorial, we'll take a closer look at the configuration options of NSURLSession and how to cancel and resume a download task. We've got a lot of ground to cover so let's get started.Read More…