Original http://www.gowhich.com/blog/view/id/382
For what reason do you want users to go to the App Store from your iOS app? Maybe you want users to go to the app Store to score your app, or you might want users to see your other iOS apps. IOS 6 introduces the Skstoreproductviewcontroller class, which allows users to display other products in the App store without leaving the current app.
Store Kit
The Skstoreproductviewcontroller class is part of the store kit framework. Skstoreproductviewcontroller is very simple to use, and it is necessary to understand some basic knowledge before explaining it in an example.
The Skstoreproductviewcontroller class is a subclass of Uiviewcontroller, and if you are familiar with the view controller, That Skstoreproductviewcontroller is very simple to use. When you want to show users the products in the App Store, you need to:
1. Instantiate a Skstoreproductviewcontroller class
2. Set its delegate
3. Display the Sotre product view controller to the consumer
The rest is handed over to the operating system. One point to keep in mind is that Skstoreproductviewcontroller can only be displayed in modal mode. The Skstoreproductviewcontrollerdelegate protocol defines a separate method-productviewcontrollerdidfinish: This method is called when the consumer leaves the App store- This is usually done by clicking the Cancel button in the top left corner of the screen. By sending the Productviewcontrollerdidfinish: message to the agent, the operating system will return control to your program. Let me show you how to use the Skstoreproductviewcontroller class in a simple program.
Step 1:setting up the Project
The app we're about to create isn't practical, it's just a button that brings users to the app Store and shows them a simple weather app I recently posted. With examples, we can see how the different parts fit together well, and how you can use the Skstoreproductviewcontroller class in your project.
Select a single View application template from the template list to create a new project in Xcode.
Set the name of the program to Usingstoreproduct, enter a company identifier, and set device family to iphone, and finally tick automatic Reference counting. Do not tick the remaining tick boxes. "Tell" Xcode where you want to save the project, click the Create button.
Step 2:adding the Store Kit Framework
Since the Skstoreproductviewcontroller class is part of the store kit framework, we need to link this store kit framework to our project. Select the project in the Project Navigator and select Target in the target list. At the top of the screen, select the Build Phase option, and then open link Binary with Libraries. Click the ' + ' button and search for Storekit in the list and select storekit.framework. This allows the store kit framework to be successfully linked to the project.
In order to use the store kit framework in the Usingstoreproductviewcontroller class, we need to enter the header file for the frame, Open the UsingStoreProductViewController.h and add the following syntax to the top of the entry:
#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 fragment. The type of the button is Uibuttontyperoundedrect, and I put the button in the middle of the view controller. I also gave this button a title and added a target-action--match UIControlEventTouchUpInside event. This means that whenever the user clicks the button, the view controller receives the message "Go to the art App Store".
- (void) viewdidload{[Super Viewdidload]; [Super Viewdidload]; //Initialize a buttonUIButton *button =[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Button Settitle:@"go to the art search 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 the Openappstore: method, I initialized the Skstoreproductviewcontroller and set myself to its delegate, and then send a loadproductwithparameters to the instance. : Completionblock: Message.
Loadproductwithparameters:completionblock: Receive two parameters:
(1) A dictionary: Use a key to specify the identifier of the program that we want to display to.
(2) a completion block.
This completion block is called when the App Store request ends. In the completed block, we want to verify that there are errors missing and present the store product view controller to the user.
Keep in mind that even if the user does not leave your program, the operating system will still be internally connected to the App Store. Because it takes a little longer to request the App Store, it's a good idea to show the user a steamer when the request has not returned a response. Once the request is completed (successful or unsuccessful), the completed block will allow us to release the activity indicator.
-(void) Openappstore: (ID) sender{//Initialize the product View ControllerSkstoreproductviewcontroller *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 app's unique identifier at itunes Connect, and each app in the App Store has a unique identifier/apple ID, note that you'll need to pass the Apple ID as a string in the parameter dictionary.
Before we build and run the program, we need the Mtviewcontroller class to follow the Skstoreproductviewcontrollerdelegate protocol by implementing the Productviewcontrollerdidfinish: Method. We can update the view controller's interface file by telling the compiler that the Usingstoreproductcontroller class complies with the Skstoreproductviewcontroller authorization protocol to see below:
#import <UIKit/UIKit.h>#import <StoreKit/StoreKit.h>@interface usingstoreproductviewcontroller:uiviewcontroller<skstoreproductviewcontrollerdelegate>@end
Step 4:build and Run
While Apple says the Skstoreproductviewcontroller class can show users other apps, it's an ideal way to let users go to 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"@interfaceUsingstoreproductviewcontroller ()@end@implementationUsingstoreproductviewcontroller@synthesizeIndicatorview;- (void) viewdidload{[Super Viewdidload]; [Super Viewdidload]; //Initialize a buttonUIButton *button =[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Button Settitle:@"go to the art search 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 the product View ControllerSkstoreproductviewcontroller *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 hideindicator]; [Self Presentviewcontroller:storeproductviewcontroller Animated:yes Completion:nil]; } }]; }-(void) Productviewcontrollerdidfinish: (Skstoreproductviewcontroller *) viewcontroller{[self Dismissviewcontrolleranimated:yes completion:nil];}- (void) showindicator{Indicatorview=[[Uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; Indicatorview.autoresizingmask=Uiviewautoresizingflexibletopmargin|Uiviewautoresizingflexiblebottommargin| Uiviewautoresizingflexibleleftmargin |Uiviewautoresizingflexiblerightmargin; [Self.view Addsubview:indicatorview]; [Indicatorview SizeToFit]; [Indicatorview startanimating]; Indicatorview.center=Self.view.center;}- (void) hideindicator{[Indicatorview stopanimating];}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
IOS 6 SDK: Show App Store "Storekit,skstoreproductviewcontroller" in App