//需导入StoreKit.frameowrk 框架
#import "ViewController.h"
#import <StoreKit/StoreKit.h>
@interface ViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>
@property (nonatomic, strong) NSArray *allProducts;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 01-请求可销售的商品列表
NSString *path = [[NSBundle mainBundle]pathForResource:@"products.json" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *allProductId = [dict valueForKeyPath:@"productId"];
SKProductsRequest *request = [[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithArray:allProductId]];
request.delegate = self;
//开始
[request start];
// 07-排到了你 支付 柜台开收据
//监听者 : 监听交易队列中的交易对象的交易状态
[[SKPaymentQueue defaultQueue]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 *product in response.products) {
NSLog(@"%@ %@ %@ %@",product.localizedTitle,product.localizedDescription,product.price,product.productIdentifier);
}
self.allProducts = response.products;
//刷新
[self.tableView reloadData];
}
// 03-展示商品 到界面上UI
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.allProducts.count;
}
//tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier= @"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
SKProduct *product = self.allProducts[indexPath.row];
//赋值
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",product.localizedTitle,product.price];
return cell;
}
// 04-用户选择购买 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//选择的商品
SKProduct *product = self.allProducts[indexPath.row];
// 05-商品开个小票 创建交易对象
SKPayment *payment = [SKPayment paymentWithProduct:product];
//SKPaymentTransaction 被添加到交易队列中的交易对象
// 06-去柜台 排队支付 添加交易对象到队列
[[SKPaymentQueue defaultQueue]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 *t in transactions) {
if (t.transactionState == SKPaymentTransactionStatePurchased) {
NSLog(@"购买成功 提供增值服务!");
[[SKPaymentQueue defaultQueue]finishTransaction:t];
}
if (t.transactionState == SKPaymentTransactionStateRestored) {
NSLog(@"恢复购买成功!");
[[SKPaymentQueue defaultQueue]finishTransaction:t];
}
}
}
- (IBAction)click:(id)sender {
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}
@end