生成productID的步骤,参考这篇教程:http://mobile.51cto.com/iphone-389249.htm ,附加一篇关于cocos2dx在IOS上实现IAP的教程:http://www.tairan.com/archives/5515 ,后者生成productID的步骤思路较清晰容易理解,建议参考后者的步骤
给出的生成productID教程中的信息已经和现在的developer.apple.com中有些出入,请灵活变通运用,生成可用的productID
以下仅仅是参考的实现代码,如有需要请酌情变更相应的内容
.h文件
导入storekit.framework
文件中很简单,建立一个简单的继承于NSObject的子类,只需要引入两个协议
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@interface IapDelegate : NSObject<SKProductsRequestDelegate, SKPaymentTransactionObserver,UIAlertViewDelegate>
- (void)methodOfShopBtn;
-------------------------------------------------------------------------------------------------------------分隔线
.m文件
这个文件中仅仅是实现两个协议的一些方法,我添加触发方法,,,,如果测试仅仅是测试,学习,可以直接把getProductInfo这个方法中红色标出的productID改成读者所生成的productID应该就可以直接运行
#import "IapDelegate.h"
#include "cocos2d.h"
@implementation IapDelegate
#pragma mark - 方法
- (void)methodOfShopBtn(整个IAP的进口,触发方法)
{
if ([SKPaymentQueue canMakePayments]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"立即开启提示功能" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"购买", nil];
[alertView show];
}
else
{
NSLog(@"用户禁止输入");
}
}
- (void)getProductInfo
{
//IOS中原生storekit调用代码
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
NSSet *set = [NSSet setWithArray:@[@"你的productID"]];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
request.delegate = self;
[request start];
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
NSString *productIdentifier = transaction.payment.productIdentifier;
NSString *receipt = [transaction.transactionReceipt base64Encoding];
if ([productIdentifier length] > 0) {
//
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"购买失败");
}
else{
NSLog(@"用户取消交易");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
#pragma mark - SKProductsRequestDelegate方法
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
if (myProduct.count == 0) {
NSLog(@"无法获取产品信息,购买失效");
return ;
}
SKPayment *payment = [SKPayment paymentWithProduct:myProduct[0]];
NSLog(@"测试:%@", payment.productIdentifier);
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
#pragma mark - skpaymentObserver方法
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:{
NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
cocos2d::UserDefault *userDefaults = cocos2d::UserDefault::getInstance();
userDefaults -> setBoolForKey("ShowSolution", YES);
[self completeTransaction:transaction];
break;
}
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
case SKPaymentTransactionStatePurchasing:
NSLog(@"商品添加进列表");
break;
default:
break;
}
}
}
#pragma mark - uialertview代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"按钮%d",buttonIndex);
if (buttonIndex == 1) {
[self getProductInfo];
}
}
@end