程序内下载App,不用跳转到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.头文件里 加上  

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #import <StoreKit/StoreKit.h>  
  2.   
  3. @interface ViewController : UIViewController<SKStoreProductViewControllerDelegate>  

3.直接在m中实现

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. - (IBAction)doAction:(UIButton *)sender {  
  2.       [self showAppInApp:@"xxxxxx"];//此处xxxxx需要替换为需要的appID  
  3. }  
  4. - (void)showAppInApp:(NSString *)_appId {  
  5.   Class isAllow = NSClassFromString(@"SKStoreProductViewController");  
  6.   if (isAllow != nil) {  
  7.     SKStoreProductViewController *sKStoreProductViewController = [[SKStoreProductViewController alloc] init];  
  8.     sKStoreProductViewController.delegate = self;  
  9.     [sKStoreProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: _appId}  
  10.                       completionBlock:^(BOOL result, NSError *error) {  
  11.                         if (result) {  
  12.                           [self presentViewController:_SKSVC  
  13.                                              animated:YES  
  14.                                            completion:nil];  
  15.                         }  
  16.                         else{  
  17.                           NSLog(@"%@",error);  
  18.                         }  
  19.                       }];  
  20.   }  
  21.   else{  
  22.     //低于iOS6没有这个类  
  23.     NSString *string = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8",_appId];  
  24.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];  
  25.   }  
  26. }  
  27.   
  28.   
  29. #pragma mark - SKStoreProductViewControllerDelegate   
  30.   
  31. //对视图消失的处理  
  32. - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {  
  33.   
  34.   
  35.   [viewController dismissViewControllerAnimated:YES  
  36.                                      completion:nil];  
  37.   
  38.   
  39. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值