内购(应用内购买)

2017-07-22  本文已影响0人  董立权

内购流程

//导入内购框架
#import <StoreKit/StoreKit.h>
#pragma mark -  1.向苹果请求可以卖的数据
//实现代理<SKProductsRequestDelegate>

    //1.1获取文件路径
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"products.json" withExtension:nil];
    //1.2获取json文件
    NSData *data = [NSData dataWithContentsOfURL:url];
    //1.3转换json数据
    NSArray *products = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    //1.4KVC直接复制
    NSArray *productIDs = [products valueForKey:@"productId"];
//    NSLog(@"%@",productIDs);
    //1.5获取产品请求对象
    SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIDs]];
    //1.6设置代理
    request.delegate = self;
    //1.7开始请求
    [request start];
#pragma mark - 2.苹果返回可以销售的商品
//可销售商品
@property (nonatomic,strong)NSArray *products;
//--------------------------------------------------
//当苹果返回可销售商品时调用
//response:返回的内容
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    for (SKProduct *product in response.products) {
        NSLog(@"商品描述:%@,商品名称:%@,商品价格:%@,商品ID:%@",product.localizedDescription,product.localizedTitle,product.price,product.productIdentifier);
    }
    //记录商品
    self.products = response.products;
    //刷新表格
    [self.tableView reloadData];
}
#pragma mark - 3.展示商品
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.products.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    //赋值
    SKProduct *product = self.products[indexPath.row];
    cell.textLabel.text = product.localizedTitle;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",product.price];
    
    return  cell;
}
#pragma mark -  4.选择商品
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取用户选择的商品
    SKProduct *product = self.products[indexPath.row];
}
#pragma mark -  5.包装成交易,加入队列
    //将商品包装成交易
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    //将交易加入队列
    [[SKPaymentQueue defaultQueue] addPayment:payment];
#pragma mark - 6.监听队列的改变
//遵循协议<SKPaymentTransactionObserver>
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
#pragma mark - 7.付款(不需要关注)
#pragma mark - 8.交易状态更新,获取用户付款状态
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
    for (SKPaymentTransaction *transaction in transactions) {
        //判断付款状态
        /*
         SKPaymentTransactionStatePurchasing 交易已被添加到队列
         SKPaymentTransactionStatePurchased  交易已经付款成功,客户端需要完成交易
         SKPaymentTransactionStateFailed     交易再被添加到服务队列之前失败或被取消
         SKPaymentTransactionStateRestored   交易被恢复,客户端需要完成交易
         SKPaymentTransactionStateDeferred   交易状态不确定
         */
        if(transaction.transactionState == SKPaymentTransactionStatePurchased) {
            NSLog(@"交易成功,提供增值服务");
#pragma mark - 9.提供增值服务,结束交易
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        }
        if(transaction.transactionState == SKPaymentTransactionStateRestored){
            NSLog(@"交易被恢复,提供增值服务");
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        }
    }
}
#pragma mark - 恢复交易
-(void)restore{
    //恢复所有的交易(非消耗)
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
上一篇下一篇

猜你喜欢

热点阅读