有的时候我们可能想让用户去App Store 为我们的应用评分,或者看到我们其他的应用。iOS 6引入了SKStoreProductViewController类,可以让用户在不离开当前应用的前提下展示App Store中的其他产品。
SKStoreProductViewController使用起来非常简单,当你希望向用户展示App Store中产品时,你需要:


注意:由于应用在请求App Store过程中,会需要稍微长的一段时间;所以最好在请求还没有返回响应时添加一个loading动画。在请求完成时(completionBlock)移除loading动画。
loadProductWithParameters:completionBlock:接收两个参数:
SKStoreProductViewController使用起来非常简单,当你希望向用户展示App Store中产品时,你需要:
- 实例化一个SKStoreProductViewController类
- 设置它的delegate
- 把sotre product视图显示给用户
SKStoreProductViewControllerDelegate协议定义了一个代理方法—productViewControllerDidFinish:,当用户离开App Store时会调用这个方法(一般是通过点击左上角画面中的取消按钮)
下面演示一下如何在一个简单的程序中使用SKStoreProductViewController类:
1、首先通过下面的两步创建一个app


2、引入framework : StoreKit.framework
3、在 ViewController.h 里引入 框架并添加代理
//
// ViewController.h
// AppStore
//
// Created by marujun on 13-4-24.
// Copyright (c) 2013年 马汝军. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface ViewController : UIViewController<SKStoreProductViewControllerDelegate>
@end
4、创建一个按钮,点击按钮打开App Store
首先在viewDidLoad里面添加一个按钮,点击按钮调用openAppStore方法。注意:由于应用在请求App Store过程中,会需要稍微长的一段时间;所以最好在请求还没有返回响应时添加一个loading动画。在请求完成时(completionBlock)移除loading动画。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 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];
}
在openAppStore: 方法中,对SKStoreProductViewController进行了初始化,并将self设置为它的delegate,然后在给这个实例发送一个loadProductWithParameters:completionBlock:消息。
loadProductWithParameters:completionBlock:接收两个参数:
- 一个id:我们想要展示给用户的app在itunes的id。
- 一个completion block。
- (void)openAppStore:(id)sender {
// Initialize Product View Controller
SKStoreProductViewController *storeProductViewController = [[SKStoreProductViewController alloc] init];
// Configure View Controller
[storeProductViewController setDelegate:self];
NSLog(@"开始进入 app store !");
[storeProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier : @"624943984"}
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];
}
}];
}
5、添加代理方法(用户离开app store 时调用)
#pragma mark - SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"已从 app store 返回本应用!");
}
6、编译运行