http://www.cocoachina.com/iphonedev/sdk/2013/0314/5820.html
文章来自:http://www.cocoachina.com/iphonedev/sdk/2013/0314/5820.html
Store Kit
SKStoreProductViewControllerDelegate
SKStoreProductViewController类是Store Kit框架的一部分。SKStoreProductViewController使用起来非常简单,在用实例讲解之前,了解一些基本的知识很有必要。
由于SKStoreProductViewController类是Store Kit框架的一部分,所以我们需要将这个Store Kit框架链接到我们的工程中。在工程导航器中选中工程,然后在target列表中选中target。在画面的顶部,选择Build Phase选项,然后打开Link Binary With Libraries。点击‘+’按钮,并在图3中的画面列表中选择StoreKit.framework。这样就可以成功的将Store Kit框架链接到工程中。
#import <StoreKit/StoreKit.h>
- (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];
}
}];
}
注意:你可以在iTunes Connect找到app的唯一识别符,App Store中的每个app都有一个唯一识别符/Apple ID,注意你需要将在参数字典中以字符串的形式传递apple id。
在生成和运行程序之前,我们需要MTViewController类通过实现productViewControllerDidFinish:方法以遵循SKStoreProductViewControllerDelegate协议。我们可以通过告诉编译器“MTViewController类符合SKStoreProductViewController授权协议”来更新view controller的接口文件,看下边:
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface MTViewController : UIViewController <SKStoreProductViewControllerDelegate>
@end
在view controller的执行文件中执行productViewControllerDidFinish:方法(如下所示),注意一点,当调用 loadProductWithParameters:completionBlock: 方法时,sotre product view controller将以模态方式显示。当用户打算离开App Store时我们负责解除(隐藏)sotre product view controller。
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}