iOS应用内展示应用并下载(StoreKit Demo)

最近就是被问到,某某浏览器可以直接在应用内展示应用并下载,还有下载进度是咋实现的,知道是iOS6的SotreKit搞的,但是还总被问有没有例子。个人觉得的应该是个很简单的东东啊?顺手写了一个:
iOS Simulator Screen shot 2013-2-2 AM11.09.27
步骤很简单:
1.添加StoreKit.framework
2.引用头文件并声明协议
#import <StoreKit/StoreKit.h>
@interface ViewController ()
@end

3.简单的几行实现代码
- (IBAction)doLoadAnApp:(UIButton *)sender {
[self openAppWithId:@"443795458"];
}

  • (void)openAppWithId:(NSString )_appId {
    Class storeVC = NSClassFromString(@"SKStoreProductViewController");
    if (storeVC != nil) {
    SKStoreProductViewController
    _SKSVC = [[SKStoreProductViewController alloc] init];
    _SKSVC.delegate = self;
    [_SKSVC loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: _appId}
    completionBlock:^(BOOL result, NSError error) {
    if (result) {
    [self presentViewController:_SKSVC
    animated:YES
    completion:nil];
    }
    else{
    NSLog(@"%@",error);
    }
    }];
    }
    else{
    //低于iOS6没有这个类
    NSString
    _idStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8",_appId];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_idStr]];
    }
    }

    pragma mark - SKStoreProductViewControllerDelegate

  • (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [viewController dismissViewControllerAnimated:YES
    completion:nil];
    }

    that's all
    顺道说下,如果你想测试下载过程的话,跟测试iAP一样,需要建立一个测试帐号,然后用那个帐号下载,不然的话会安装不成功
    再有就是,明显那个“低于iOS6”就是指iOS5喽,因为这类只提供了block实现,就只能用在4.x以上了,而现在苹果要求4.x只支持4.3,所以是不是很多人都放弃4.3只支持5.x以上了?

工程下载:StoreKitTest


相关博文

About rainbird

IOS攻城狮
This entry was posted in iOS, object-c and tagged , , . Bookmark the permalink.

发表评论