閑來沒事看了篇文章 應用内建立應用商店環境,不跳轉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
@interface ViewController : UIViewController<</span>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];