iOS 内购的实现思路

#import "ViewController.h"

#import <StoreKit/StoreKit.h>

@interface ViewController ()<SKProductsRequestDelegate,UITableViewDataSource,UITableViewDelegate,SKPaymentTransactionObserver>

//协议规则  告诉对方到时候需要实现哪些方法

@property (weak, nonatomic) IBOutletUITableView *tableView;

@property (nonatomic,strong) NSArray *allProducts;

@end


@implementation ViewController

- (IBAction)btnClick:(id)sender {

    

    [[SKPaymentQueuedefaultQueue]restoreCompletedTransactions];

}


- (void)viewDidLoad {

    [superviewDidLoad];

   

//    self.tableView.delegate = self;

//    self.tableView.dataSource = self;

    //0.上传自己需要销售商品列表

    

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

    

    NSData *data = [NSDatadataWithContentsOfFile:path];

    

    NSArray *allData = [NSJSONSerializationJSONObjectWithData:data options:0error:nil];

    

    

    NSArray *allProduct = [allData valueForKeyPath:@"productId"];

    //1.请求可销售的商品列表

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

    //返回数据 传址  代理  block 回调

    [request start];

    

    request.delegate = self;

    

    

    //5.添加监听者

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

    [[SKPaymentQueuedefaultQueue]addTransactionObserver:self];

    //6.监听到交易队列中交易对象的交易状态的改变

    

    //7.交易成功的话提供增值服务


    

    

}

//请求到可以销售的商品列表的时候就会调用

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

{

    for (SKProduct *productin response.products) {

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

    }

    self.allProducts = response.products;

    

    [self.tableViewreloadData];

}


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

{

    returnself.allProducts.count;

}


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

    

    static NSString *ID =@"cell";

    

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

    

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];

    }

    

    //获取模型数据

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

    //赋值

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

    

    

    return cell;

}

//2.用户选择购买

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

{

    //3.创建交易对象封装信息  名称和价格 小票

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

    

    SKPayment *payment= [SKPaymentpaymentWithProduct:product];

    //4.添加交易对象到交易队列中

    [[SKPaymentQueuedefaultQueue]addPayment:payment];


}

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

        }

    }

    

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值