标签:
ios内购实现代码如下():
#import "ViewController.h"
#import <StoreKit/StoreKit.h>
@interface ViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>
//记录产品列表
@property(nonatomic,strong)NSArray*allProducts;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1 请求可销售商品的列表
//准备需要销售的商品列表
NSString *path=[[NSBundle mainBundle]pathForResource:@"products.json" ofType:nil];
NSData *data=[NSData dataWithContentsOfFile:path];
NSArray *arrayList=[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//产品列表
NSArray *arrayIdList=[arrayList valueForKeyPath:@"productId"];
NSSet *set=[NSSet setWithArray:arrayIdList];
//创建一个请求对象
SKProductsRequest*request=[[SKProductsRequest alloc]initWithProductIdentifiers:set];
//设置代理
request.delegate=self;
//启动请求
[request start];
//创建监听对象 来监听交易队列中交易对象的交易状态
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
}
//代理方法,返回请求对象的数据列表
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
for (SKProduct*product in response.products) {
//对象中包含商品对象的所有信息
}
self.allProducts=response.products;
//刷新表格
[self.tableView reloadData];
}
//2.展示可销售商品
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.allProducts.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
SKProduct *product=self.allProducts[indexPath.row];
cell.textLabel.text=product.localizedTitle;
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@圆",product.price];
return cell;
}
//3点击购买
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//4开票
SKProduct *pro=self.allProducts[indexPath.row];
SKPayment *payment=[SKPayment paymentWithProduct:pro];
//5,创建交易对象,添加到交易队列
//NSString *str=[NSBundle mainBundle]pathForResource:@"blue.bundle/blue/" ofType:nil;
[[SKPaymentQueue defaultQueue]addPayment:payment];
}
//监听交易队列 中 的交易状态 改变的时候就会调用
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
//非消耗品 才能被恢复购买
/*
SKPaymentTransactionStatePurchasing, 交易正在被添加到交易队列
SKPaymentTransactionStatePurchased, //交易已经在队列,用户已经付钱,客户端需要完成交易
SKPaymentTransactionStateFailed, //还没添加到队列中就取消或者失败了
SKPaymentTransactionStateRestored, // 交易被恢复购买,客户端需要完成交易
SKPaymentTransactionStateDeferred NS_ENUM_AVAILABLE_IOS(8_0), 交易在队列中,交易状态不确定依赖别的参数参与
*/
//7.如果交易成功提供 交易的产品
for(SKPaymentTransaction *t in transactions)
{
if (t.transactionState==SKPaymentTransactionStatePurchased) {
//提供服务
//结束交易
[[SKPaymentQueue defaultQueue]finishTransaction:t];
}else if(t.transactionState==SKPaymentTransactionStateRestored)
{
//恢复购买成功
//结束交易
[[SKPaymentQueue defaultQueue]finishTransaction:t];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[[SKPaymentQueue defaultQueue]removeTransactionObserver:self];
}
标签:
原文地址:http://www.cnblogs.com/tangranyang/p/4643756.html