版权声明:本文为Jumbo原创文章,采用[知识共享 署名-非商业性使用-禁止演绎 4.0 国际 许可协议],转载前请保证理解此协议
原文出处:https://www.jianshu.com/p/40da3c2730ed
需要监听内购的地方加上(一般初始化的时候):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rechargeResult:) name:kIAPNotification_Recharge object:nil];
注销监听内购的地方加上(一般销毁的时候):
[[NSNotificationCenter defaultCenter] removeObserver:kIAPNotification_Recharge];
支付:buy 产品ID 需要在苹果开发这后台添加商品ID
回调:处理支付结果 1、_buy_ok 2、_buy_failed
IAP.h
#import <StoreKit/StoreKit.h>
extern NSString *const kIAPNotification_Recharge;
@interface IAP_Delegate : NSObject<SKProductsRequestDelegate, SKPaymentTransactionObserver>
- (id)init;
- (void)dealloc;
- (void)buy:(const char *)product;
- (void)_buy_ok:(SKPaymentTransaction *)transaction;
- (void)_buy_failed:(SKPaymentTransaction *)transaction;
- (void)_buy_restored:(SKPaymentTransaction *)transaction;
@end
IAP.mm
#import "IAP.h"
NSString *const kIAPNotification_Recharge = @"IAPNotification_Recharge";
@implementation IAP_Delegate
- (id)init
{
self = [super init];
if (self) {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
return self;
}
- (void)dealloc
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[super dealloc];
}
- (void)buy:(const char *)product
{
NSSet *set = [NSSet setWithArray:@[[NSString stringWithUTF8String:product]]];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
[request setDelegate:self];
[request start];
}
- (void)_buy_ok:(SKPaymentTransaction *)transaction
{
NSString *product = transaction.payment.productIdentifier;
NSString *receipt = nil;
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0
receipt = [transaction.transactionReceipt base64Encoding];
#else
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
receipt = [[NSData dataWithContentsOfURL:receiptURL] base64EncodedStringWithOptions:0];
#endif
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"0",@"result",product,@"product", receipt, @"receipt",nil];
[[NSNotificationCenter defaultCenter]postNotificationName:kIAPNotification_Recharge object:self userInfo:dict];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)_buy_failed:(SKPaymentTransaction *)transaction
{
NSString *product = transaction.payment.productIdentifier;
if (transaction.error.code == SKErrorPaymentCancelled) {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"result",product,@"product",nil];
[[NSNotificationCenter defaultCenter]postNotificationName:kIAPNotification_Recharge object:self userInfo:dict];
} else {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"result",product,@"product",nil];
[[NSNotificationCenter defaultCenter]postNotificationName:kIAPNotification_Recharge object:self userInfo:dict];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)_buy_restored:(SKPaymentTransaction *)transaction
{
NSString *product = transaction.payment.productIdentifier;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"result",product,@"product",nil];
[[NSNotificationCenter defaultCenter]postNotificationName:kIAPNotification_Recharge object:self userInfo:dict];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
#pragma mark -
#pragma mark SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"---------收到产品反馈信息---------");
NSArray *pids = [response invalidProductIdentifiers];
NSLog(@"产品Product ID:%@", pids);
NSLog(@"产品付费数量:%d", (int)[[response products] count]);
if (![[response products] count]) {
NSString *product = 0;
if ([pids count]) {
product = (NSString *)[pids objectAtIndex:0];
}
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"result",product,@"product",nil];
[[NSNotificationCenter defaultCenter]postNotificationName:kIAPNotification_Recharge object:self userInfo:dict];
return ;
}
NSLog(@"-------------发送购买请求----------------");
SKPayment *payment = [SKPayment paymentWithProduct:[[response products] objectAtIndex:0]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
#pragma mark -
#pragma mark SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
// do nothing.
break;
case SKPaymentTransactionStatePurchased:
[self _buy_ok:transaction];
break;
case SKPaymentTransactionStateFailed:
[self _buy_failed:transaction];
break;
case SKPaymentTransactionStateRestored:
[self _buy_restored:transaction];
break;
case SKPaymentTransactionStateDeferred:
// i don't know...
break;
}
}
}
@end