首页 > 代码库 > 苹果开发——App内购以及验证store的收据(二)
苹果开发——App内购以及验证store的收据(二)
原地址:http://zengwu3915.blog.163.com/blog/static/2783489720137605156966?suggestedreading
//
// StoreObserver.h
// HelloStore
//
// Created by guoke on 13-8-4.
// Copyright (c) 2013年 guoke. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
#import <StoreKit/SKPaymentTransaction.h>
@interface StoreObserver : NSObject <SKPaymentTransactionObserver, SKProductsRequestDelegate>
{
int buyType;
}
-(void)Create;
-(void)Destroy;
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
-(void)PurchasedTransaction: (SKPaymentTransaction *)transaction;
-(void)completeTransaction: (SKPaymentTransaction *)transaction;
-(void)failedTransaction: (SKPaymentTransaction *)transaction;
-(void)paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;
-(void)paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;
-(void)Buy:(int)type;
-(bool)CanMakePay;
-(void)RequestProductData;
-(void)restoreTransaction: (SKPaymentTransaction *)transaction;
@end
//
// StoreObserver.m
// HelloStore
//
// Created by guoke on 13-8-4.
// Copyright (c) 2013年 guoke. All rights reserved.
//
#import "StoreObserver.h"
#define ProductID @"com.HelloStorTest.TestItem" // 产品ID,对应iTunes Connect中设置的产品ID
@implementation StoreObserver
-(void)Create
{
//---------------------
//----监听购买结果
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
-(void)Destroy
{
//解除监听
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
-(void)Buy:(int)type
{
buyType = type;
if ([self CanMakePay])
{
NSLog(@"允许程序内付费购买");
[self RequestProductData];
}
else
{
NSLog(@"不允许程序内付费购买");
}
}
-(bool)CanMakePay
{
return [SKPaymentQueue canMakePayments];
}
-(void)RequestProductData
{
NSLog(@"---------请求对应的产品信息------------");
NSArray *product = nil;
// 这时可以根据buyType不同选择不同的商品,这个只是测试,只用了一种
product=[[NSArray alloc] initWithObjects:ProductID,nil];
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
request.delegate=self;
[request start];
[product release];
}
//<SKProductsRequestDelegate> 请求协议
//收到的产品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"-----------收到产品反馈信息--------------");
NSArray *myProduct = response.products;
NSLog(@"产品Product ID:%@",response.invalidProductIdentifiers);
NSLog(@"产品付费数量: %d", [myProduct count]);
// populate UI