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、在真機上測試,即可實現效果。