IOS6.0 應用內直接下載程序 不需跳轉AppStore -b

閒來沒事看了篇文章 應用內建立應用商店環境,不跳轉AppStore. 先武斷的想一句:放屁。而後好奇的進去看看,原來是IOS6.0的新特性,頓感慚愧。研究下app

 

SKStoreProductViewController類是UIViewController的子類, 若是你對view controller比較熟悉的話,那SKStoreProductViewController使用起來也很是簡單了。當你但願向用戶展現App Store中產品時,你須要:spa

1.實例化一個SKStoreProductViewController類
2.設置它的delegate
3.把sotre product視圖控制器顯示給消費者操作系統

 

剩下的就交給操做系統來處理了。須要記住一點的是SKStoreProductViewController只能以模態的方式顯示。SKStoreProductViewControllerDelegate協議定義了一個單獨的方法—productViewControllerDidFinish:,當消費者離開App Store時會調用這個方法—通常是經過點擊左上角畫面中的取消按鈕。經過給代理髮送productViewControllerDidFinish:消息,操做系統就會把控制權返回到你的程序。固然你不能忘了 只支持IOS6.0及其以上~~代理

 

步驟:orm

1.添加 storeKit.frameworkstring

2.頭文件裏 加上  產品

#import it

@interface ViewController : UIViewController<</span>SKStoreProductViewControllerDelegate>io

3.直接在m中實現import

- (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];

}

相關文章
相關標籤/搜索