#import
@interface IAPUnity : NSObject
@property(retain,nonatomic)NSArray* products; //产品列表
@property(retain,nonatomic)NSSet* productIdf; //产品标记符
@property(retain,nonatomic)SKProductsRequest* request; //用于请求相关信息
///获取商店信息
-(void)requestStart;
///初始化
- (id)initProductIdf:(NSSet*)productIdf;
///购买
-(void)buyProduct;
///恢复交易
-(void)restorer;
@end
#import "IAPUnity.h"
@interface IAPUnity ()
@property(retain,nonatomic)SKProduct* product;
@end
@implementation IAPUnity
- (id)initProductIdf:(NSSet*)productIdf
{
self = [super init];
if (self) {
self.productIdf=productIdf;
[self getProducts];
}
return self;
}
-(void)getProducts
{
_request=[[SKProductsRequest alloc]initWithProductIdentifiers:_productIdf];
}
-(void)requestStart
{
[_request setDelegate:self];
///请求数据
[_request start];
}
#pragma SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
///判断用户是否禁用内购功能
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"%@",response.products);
//得到产品列表
self.products=response.products;
}
///获取成功列表成功,并且取到第0个的相关信息
if ([response.products count]>0) {
self.product=[response.products objectAtIndex:0];
NSLog(@"产品本地标题:%@",[self.product localizedTitle]);
NSLog(@"产品描述:%@",self.product.localizedDescription);
NSLog(@"产品价格:%@",self.product.price);
NSLog(@"产品标识:%@",self.product.productIdentifier);
}
///发生错误的时候输出错误信息
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
NSLog(@"Invalid product id: %@" , invalidProductId);
}
}
-(void)buyProduct
{
if (self.product) {
if ([SKPaymentQueue canMakePayments]) {
SKPayment* payment=[SKPayment paymentWithProduct:self.product];
///为支付队列增加一个观察者
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
///增加了一个观察者到付款队列中
[[SKPaymentQueue defaultQueue]addPayment:payment];
}
}
}
-(void)restorer
{
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}
// Sent when the transaction array has changed (additions or state changes). Client should check state of transactions and finish as appropriate.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction* transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
{
NSLog(@"购买中");
}
break;
case SKPaymentTransactionStatePurchased:
{
NSLog(@"SKPaymentTransactionStatePurchased");
NSLog(@"%@",[[NSString alloc]initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding]);
NSString* temp=[[NSString alloc]initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
UIAlertView* alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"内购成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
NSLog(@"BASE64:%@",[self encode:temp.UTF8String length:temp.length]);
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
break;
case SKPaymentTransactionStateFailed:
{
if (transaction.error.code!=SKErrorPaymentCancelled) {
NSLog(@"Failed....%d",transaction.error.code);
NSLog(@"%@",transaction.error.localizedDescription);
}
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
break;
case SKPaymentTransactionStateRestored:
{
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
break;
default:
break;
}
}
}
@end
之前的BASE64函数有点问题,所以删掉了,换了一个
下面是 transaction.transactionState的几种状态
SKPaymentTransactionStatePurchasing, // 这一个是应用商店是正在处理交易的状态
SKPaymentTransactionStatePurchased, // 这一个是应用商店成功处理付款的状态
SKPaymentTransactionStateFailed, // 这儿是应用商店处理失败的状态
SKPaymentTransactionStateRestored // 这个是应用商店恢复之前的状态
著作权声明:本文由 http://blog.csdn.net/dalehui/ 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!