IOS displays App Store [StoreKit, SKStoreProductViewController], iosstorekit

Source: Internet
Author: User

IOS displays App Store [StoreKit, SKStoreProductViewController], iosstorekit

For what reason do you want users to access the app Store from your iOS App? You may want the user to go To the App Store to rate your app, or you may want the user to see your other iOS apps. IOS 6 introduces the SKStoreProductViewController class, which allows you to display other products in the App Store without leaving the current application.

Store Kit

The SKStoreProductViewController class is part of the Store Kit framework. SKStoreProductViewController is easy to use. It is necessary to know some basic knowledge before using instances.

The SKStoreProductViewController class is a subclass of UIViewController. If you are familiar with view controller, SKStoreProductViewController is easy to use. When you want to show users the products in the App Store, You need:

1. instantiate a SKStoreProductViewController class
2. Set its delegate
3. display the sotre product View Controller to the consumer.

The rest will be handled by the operating system. Note that SKStoreProductViewController can only be displayed in mode. The SKStoreProductViewControllerDelegate Protocol defines a separate method-productViewControllerDidFinish: This method is called when the consumer leaves the App Store-generally by clicking the cancel button in the upper left corner of the screen. By sending the productViewControllerDidFinish: Message to the proxy, the operating system returns the control to your program. Next I will demonstrate how to use the SKStoreProductViewController class in a simple program.

Step 1: Setting Up the Project

The app we are going to create is not very practical. There is only one button that can bring users into the App Store and show users a simple weather app I recently released. Through the instance, we can understand how different parts fit well together, and how to use the SKStoreProductViewController class in the project.

Select a Single View Application template from the template list and create a new project in Xcode.

Set the program name to UsingStoreProduct, enter a company identifier, set device family to iPhone, and then select Automatic Reference Counting. Do not select the remaining check boxes. "Tell" Xcode where you want to save the project and click "CREATE.

Step 2: Adding the Store Kit Framework

Because the SKStoreProductViewController class is part of the Store Kit framework, we need to link this Store Kit framework to our project. Select a project in the project navigator, and then select target in the target list. On the top of the screen, select the Build Phase option and enable Link Binary With Libraries. Click '+', search for StoreKit in the list, and select StoreKit. framework. In this way, the Store Kit framework can be successfully linked to the project.

To use the Store Kit framework in the UsingStoreProductViewController class, we need to enter the header file of the Framework, open UsingStoreProductViewController. h, and add the following syntax at the top:

#import <StoreKit/StoreKit.h>
Step 3: Using the SKStoreProductViewController Class

In the viewDidLoad method of the View Controller, create a new button in the following code snippet. The button type is UIButtonTypeRoundedRect, And I place this button in the middle of the view Controller view. At the same time, I also created a title for this button and added a target-action-matching UIControlEventTouchUpInside event. This means that whenever you click a button, view controller will receive the message "go to the App Store.

-(Void) viewDidLoad {[super viewDidLoad]; [super viewDidLoad]; // initialize a button UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setTitle: @ "go to the App Store" forState: UIControlStateNormal]; [button setFrame: CGRectMake (0.0, 0.0, 200.0, 44.0)]; [button setCenter: self. view. center]; [self. view addSubview: button]; [button addTarget: self action: @ selector (openAppStore :) forControlEvents: UIControlEventTouchUpInside];}

In openAppStore: method, I initialized SKStoreProductViewController, set myself as its delegate, and then sent a loadProductWithParameters: completionBlock: message to this instance.
LoadProductWithParameters: completionBlock: receives two parameters:

(1) A dictionary: Use a key to specify the identifier of the program we want to display.

(2) A completion block.
This completion block is called when the App store request ends. In the completed block, we need to verify whether there are any errors and omissions and display the store product View Controller to users.

Remember that even if the user does not leave your program, the operating system will still be connected to the App store internally. It takes a relatively long time to request the App Store. That is to say, it is best to display a fire wheel to the user when the request has not returned a response. Once the request is completed (successful or unsuccessful), the completed block will allow us to remove the activity indicator.

-(Void) openAppStore :( id) sender {// initialize Product View Controller SKStoreProductViewController * storeProductViewController = [[SKStoreProductViewController alloc] init]; // configure View Controller [storeProductViewController setDelegate: self]; [storeProductViewController loadProductWithParameters: @ {SKStoreProductParameterITunesItemIdentifier: @ "685836302"} completionBlock: ^ (BOOL result, NSError * error) {if (error) {NSLog (@ "Error % @ with User Info % @. ", error, [error userInfo]);} else {[self presentViewController: storeProductViewController animated: YES completion: nil] ;}}];}

Note: you can find the unique identifier of the app in iTunes Connect. Each App in the app Store has a unique identifier/Apple ID, note that you need to pass the apple id as a string in the parameter dictionary.

Before generating and running the program, we need the MTViewController class to implement the productViewControllerDidFinish: Method to follow the SKStoreProductViewControllerDelegate protocol. We can update the view controller Interface file by telling the compiler "UsingStoreProductController class complies with SKStoreProductViewController authorization protocol". See the following:

#import <UIKit/UIKit.h>#import <StoreKit/StoreKit.h>@interface UsingStoreProductViewController : UIViewController<SKStoreProductViewControllerDelegate>@end
Step 4: Build and Run

Although Apple indicates that the SKStoreProductViewController class can display other apps to users, this is an ideal way to let users score in the app Store without leaving the current app.

The entire running logic code:

UsingStoreProductViewController. m

/// UsingStoreProductViewController. m // UsingStoreProduct /// Created by david on 13-9-23. // Copyright (c) 2013 WalkerFree. all rights reserved. // # import "UsingStoreProductViewController. h "@ interface UsingStoreProductViewController () @ end @ implementation UsingStoreProductViewController @ synthesize indicatorView;-(void) viewDidLoad {[super viewDidLoad]; [super viewDidLoad]; // initialize a button UIButton * button = [UIButton buttonWithType: Custom]; [button setTitle: @ "go to the App Store" forState: UIControlStateNormal]; [button setFrame: CGRectMake (0.0, 0.0, 200.0, 44.0)]; [button setCenter: self. view. center]; [self. view addSubview: button]; [button addTarget: self action: @ selector (openAppStore :) forControlEvents: UIControlEventTouchUpInside];}-(void) openAppStore :( id) sender {[self showIndicator]; // initialize Product View Controller SKStoreProductViewController * storeProductViewController = [[SKStoreProductViewController alloc] init]; // configure View Controller [storeProductViewController setDelegate: self]; [storeProductViewController loadProductWithParameters :@{ parameters: @ "685836302"} completionBlock: ^ (BOOL result, NSError * error) {if (error) {NSLog (@ "Error % @ with User Info % @. ", error, [error userInfo]);} else {[self hideIndicator]; [self presentViewController: storeProductViewController animated: YES completion: nil] ;}];}-(void) productViewControllerDidFinish :( SKStoreProductViewController *) viewController {[self defined: YES completion: nil];}-(void) showIndicator {indicatorView = [[externalloc] callback: callback]; indicatorView. autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | bytes | UIViewAutoresizingFlexibleRightMargin; [self. view addSubview: indicatorView]; [indicatorView sizeToFit]; [indicatorView startAnimating]; indicatorView. center = self. view. center;}-(void) hideIndicator {[indicatorView stopAnimating];}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.