1、新建工程,导入库文件:StoreKit.framework,并且导入头文件,如下:
#import <StoreKit/StoreKit.h>
2、在需要点击事件中,添加代码,如下:
- (IBAction)gotoStore:(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];
}
}];
}
3、设置协议 SKStoreProductViewControllerDelegate,并且实习协议中的方法,如下:
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
4、在真机上测试,即可实现效果。