閒來沒事看了篇文章 應用內創建應用商店環境,不跳轉AppStore. 先武斷的想一句:放屁。然後好奇的進去看看,原來是IOS6.0的新特性,頓感慚愧。研究下
SKStoreProductViewController類是UIViewController的子類, 如果你對view controller比較熟悉的話,那SKStoreProductViewController使用起來也非常簡單了。當你希望向用戶展示App Store中產品時,你需要:
1.實例化一個SKStoreProductViewController類
2.設置它的delegate
3.把sotre product視圖控制器顯示給消費者
剩下的就交給操作系統來處理了。需要記住一點的是SKStoreProductViewController只能以模態的方式顯示。SKStoreProductViewControllerDelegate協議定義了一個單獨的方法—productViewControllerDidFinish:,當消費者離開App Store時會調用這個方法—一般是通過點擊左上角畫面中的取消按鈕。通過給代理髮送productViewControllerDidFinish:消息,操作系統就會把控制權返回到你的程序。當然你不能忘了 只支持IOS6.0及其以上~~
步驟:
1.添加 storeKit.framework
2.頭文件裏 加上
#import <StoreKit/StoreKit.h>
@interface ViewController : UIViewController<SKStoreProductViewControllerDelegate>
3.直接在m中實現
- (IBAction)doAction:(UIButton *)sender {
[self showAppInApp:@"xxxxxx"];//此處xxxxx需要替換爲需要的appID
}
- (void)showAppInApp:(NSString *)_appId {
Class isAllow = NSClassFromString(@"SKStoreProductViewController");
if (isAllow != nil) {
SKStoreProductViewController *sKStoreProductViewController = [[SKStoreProductViewController alloc] init];
sKStoreProductViewController.delegate = self;
[sKStoreProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: _appId}
completionBlock:^(BOOL result, NSError *error) {
if (result) {
[self presentViewController:_SKSVC
animated:YES
completion:nil];
}
else{
NSLog(@"%@",error);
}
}];
}
else{
//低於iOS6沒有這個類
NSString *string = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8",_appId];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}
}
#pragma mark - SKStoreProductViewControllerDelegate
//對視圖消失的處理
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES
completion:nil];
}