首先你必须熟悉内支付的流程,可以到网上搜搜看。
前几天因为需要做了个苹果的内支付,当中遇到了不少苦头,感觉网上的各种博客对内支付这一块都是点到即止,没有真真正正实现自己的需求。正所谓辛苦就有所得,通过东接西搬,终于内力小成,特与大家分享一下自己的经验。这里我假设你已经把商品的ID什么的都已经注册成功了,测试账号也有了。
下面直接贴代码了~~~~
//
// IAPHelperViewController.h
//
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface IAPHelperViewController : UIViewController<SKProductsRequestDelegate,SKPaymentTransactionObserver>
{
NSString* kMyInAppIdentifier;
BOOL receiptValid;
UIActivityIndicatorView* spinner;
}
+ (IAPHelperViewController *) sharedIAPHelper;
- (void) requestProUpgradeProductData;
- (void) requestProductData;
- (void) buyGolds:(int)buyType;
- (BOOL) canInAppPayment;
- (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) restoreTransaction:(SKPaymentTransaction *)transaction;
- (void) recordTransaction:(SKPaymentTransaction *)product;
- (void) verifyReceiptFromMyServer:(SKPaymentTransaction *)transaction;
@end
//
// IAPHelperViewController.m
//
#import "IAPHelperViewController.h"
#include "cocos2d.h"
using namespace cocos2d;
const static int IAP1p99 = flag1;//主要是标记用户点击了那个商品
const static int IAP2p99 = flag2;
const static int IAP3p99 = flag3;
const static int IAP4p99 = flag4;
const static int IAP5p99 = flag5;
#define MyServerURL @"your server address , not apple"
#define ProductOne @"product 1 id"
#define ProductThree @"....... 2 .."
#define ProductFive @"....... 3 .."
#define ProductFifteen @"....... 4 .."
#define ProductFifty @"...... 5 .."
#define MyFrint NSLog(@"\n%s %d\n",__FUNCTION__, __LINE__)
@interface IAPHelperViewController ()
@end
@implementation IAPHelperViewController
//定义单例变量
static IAPHelperViewController* singleton = nil;
//单例模式
+ (IAPHelperViewController *) sharedIAPHelper
{
MyFrint;
if (singleton == nil) {
singleton = [[IAPHelperViewController alloc] initWithNibName:nil bundle:nil];
}
return singleton;
}
//初始化 这里应该根据自己的情况初始化 我用的是C++写的界面 OC不鸟~~~~~~
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
MyFrint;
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
receiptValid = NO;
}
r