关于iOS内购的一些代码整理分析

来源:互联网 发布:怎样成为淘宝直播 编辑:程序博客网 时间:2024/04/27 14:03

//需导入StoreKit.frameowrk 框架

#import "ViewController.h"

#import <StoreKit/StoreKit.h>

@interface ViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>

@property (nonatomic,strong) NSArray *allProducts;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

//    01-请求可销售的商品列表

    

    NSString *path = [[NSBundlemainBundle]pathForResource:@"products.json"ofType:nil];

   NSData *data = [NSDatadataWithContentsOfFile:path];

    

    NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:dataoptions:0error:nil];

   NSArray *allProductId = [dict valueForKeyPath:@"productId"];

    

    SKProductsRequest *request = [[SKProductsRequestalloc]initWithProductIdentifiers:[NSSetsetWithArray:allProductId]];

    request.delegate =self;

   //开始

    [requeststart];



    //    07-排到了你 支付 柜台开收据

    //监听者  : 监听交易队列中的交易对象的交易状态

    [[SKPaymentQueuedefaultQueue]addTransactionObserver:self];

    //08-去商店取得商品 提供增值服务

  

}

#pragma mark -  SKProductsRequest

// Sent immediately before -requestDidFinish:

//    02-返回可以销售的商品列表

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response NS_AVAILABLE_IOS(3_0);

{

   for (SKProduct *productin response.products) {

        NSLog(@"%@  %@ %@ %@",product.localizedTitle,product.localizedDescription,product.price,product.productIdentifier);

    }


   self.allProducts = response.products;

    

   //刷新

    [self.tableViewreloadData];

}


//    03-展示商品  到界面上UI

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    returnself.allProducts.count;

}

//tableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString *identifier=@"cell";

    

   UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

    

   if (cell==nil) {

        cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];

    }

   SKProduct *product = self.allProducts[indexPath.row];

    

   //赋值

    cell.textLabel.text = [NSStringstringWithFormat:@"%@ %@",product.localizedTitle,product.price];

    

   return cell;

}

//    04-用户选择购买  点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    //选择的商品

   SKProduct *product = self.allProducts[indexPath.row];

    //    05-商品开个小票 创建交易对象

   SKPayment *payment = [SKPaymentpaymentWithProduct:product];

    //SKPaymentTransaction  被添加到交易队列中的交易对象

    //    06-去柜台排队支付  添加交易对象到队列

    [[SKPaymentQueuedefaultQueue]addPayment:payment];


}

#pragma mark - SKPaymentTransactionObserver //NS_AVAILABLE_IOS(3_0);

//交易对象的交易状态改变的时候就会调用

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions

{

   /*

     SKPaymentTransactionStatePurchasing,    // 交易正在被添加到交易队列中

     SKPaymentTransactionStatePurchased,     // 交易已经在队列中, 用户付钱了客户需要完成交易.

     SKPaymentTransactionStateFailed,        // 交易失败 交易不再队列.

     SKPaymentTransactionStateRestored,      // 交易被重新购买客户端需要完成交易.

     SKPaymentTransactionStateDeferred NS_ENUM_AVAILABLE_IOS(8_0),   // 交易在队列中, 最终状态不确定.

     */

   //成功

   for (SKPaymentTransaction *tin transactions) {

        if (t.transactionState ==SKPaymentTransactionStatePurchased) {

           NSLog(@"购买成功提供增值服务!");

            [[SKPaymentQueuedefaultQueue]finishTransaction:t];

        }

        if (t.transactionState ==SKPaymentTransactionStateRestored) {

           NSLog(@"恢复购买成功!");

            [[SKPaymentQueuedefaultQueue]finishTransaction:t];

        }

    }

    

}


- (IBAction)click:(id)sender {

    

    [[SKPaymentQueuedefaultQueue]restoreCompletedTransactions];

}



@end


0 0
原创粉丝点击