Himi  原创, 欢迎转载,转载请在明显处注明! 谢谢。

原文地址:http://blog.csdn.net/xiaominghimi/article/details/6937097

//——2012-12-11日更新   获取"产品付费数量等于0这个问题"的原因

看到很多童鞋问到,为什么每次都返回数量等于0??

其实有童鞋已经找到原因了,原因是你在 ItunesConnect 里的 “Contracts, Tax, and Banking ”没有完成设置账户信息。

确定 ItunesConnect 里 “Contracts, Tax, and Banking ”的状态,如下图所示,即可:


这里也是由于Himi疏忽的原因没有说明,这里先给童鞋们带来的麻烦,致以歉意。

//——2012-6-25日更新iap恢复

看到很多童鞋说让Himi讲解如何恢复iap产品,其实博文已经给出了。这里再详细说下:

首先向AppStore请求恢复交易:

1
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

然后当用户输入正确的appStore账号密码后,进入

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易结果

进入上面函数中的

1
2
3
4
5
6
case SKPaymentTransactionStateRestored://恢复
            {
                [self restoreTransaction:transaction];
            }
                break;

然后我们再以下重写函数中处理即可!

- (void) restoreTransaction: (SKPaymentTransaction *)transaction

//——-

终于在11月公司的游戏即将上线了,那么对于iOS游戏来说当今都是内置道具收费属于主流,那么我们的游戏也是内置收费,所以Himi这里分享给大家关于内置应用收费以及申请测试账号进行测试购买的经验;

在应用内嵌入付费代码这一快Himi可以直接将代码分享给大家,所以我们来说一些主要流程,毕竟没有接触过这一块的童鞋肯定相当头疼 =。  =

OK,步入整体,如果你想在iOS里内嵌收费,那么分为以下几步:

 【提示:以下创建App部分内容,你不用非要等项目能打包了才开始做,可以随时并且随便的创建个测试项目即可,因为嵌入付费并不要求上传App的ipa包的!!】   

第一步:你需要在iTunesConnect中创建个新的App,然后为这个App设置一些产品(付费道具)等;

OK,这里Himi稍微解释下,iTunesConnect是苹果提供的一个平台,主要提供AP发布和管理App的,最重要的功能是创建管理项目信息,项目付费产品(道具)管理、付费的测试账号、提交App等等,这里就简单介绍这么多,关于产品一词在此我们可以理解成游戏道具即可;在苹果看来所有付费都属于产品 =。 =千万不要纠结字眼哦~

OK,打开iTunesConnect网站:https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa (注意:企业级的用户必须使用公司主开发者账号登陆才可!)

成功登陆后的页面如下:

这里大概说下重要的一些项:

 Contracts, Tax, and Banking   : 管理银行账号、联系人以及税等等;这里要根据提示完成对应的信息填写!一定要详细填写喔~

             Manage Users :管理用户的,比如主账号以及测试付费的(测试App)账号;

             Manage Your Applictions:管理应用程序的,你所有发布的应用和每个应用的状态都在这里面;

下面我们新建一个App项目,大家放心,我们这里创建的是不会直接提交给App审核的,所以放心创建,只要控制好App的状态不要是待审核状态即可,不过即使你不小心将项目提交了,也没事,直接更改App状态即可了;

选择Manage Your Applictions选项,然后新建一个项目:【Add New App】,根据提示来填写吧,这里就不细致说明了~

创建好一个App之后,在点击Manage Your Applictions后的界面应该如下:

这里你将看到自己创建的App,点击你创建的App项目,这里Himi创建的项目名字叫”ProjectForBuyTest“,点击你的App进入如下界面:

 

  (注意:这里的Bundle ID一定要跟你的项目中的info.plist中的Bundle ID保证一致!!!!)

这里可以管理你的项目的信息、状态、是否嵌入GameCenter等等选项,那么本章我们重点介绍如何使用IAp沙盒测试程序内付费,所以这里我们点击右上角的”Manage In-App Purchases“选项进入创建产品(游戏道具)界面如下:

上图中的下方看到Himi创建过的四个产品(道具)了,你可以点击”Create New“选项新建一个产品(付费道具),点击新建如下界面:

上图中Himi没有截图出所有的选项,这里大概介绍下,这个界面是选择你的消费道具的种类,种类说明如下:

   类型选择有四种选择:

   1.Consumable(消耗品): 每次下载都需要付费;

   2.Non-consumable(非消耗品): 仅需付费一次;

   3.Auto-Renewable Subscriptions:自动订阅;

   4.Free Subscription:免费订阅

   最下方是你沙盒测试的截图,暂且不管即可;

   这里Himi选择Consumable选项,比如很多游戏都是购买金币啦这样子就可以选择这个;然后出现如下界面:

Reference Name: 付费产品(道具的)参考名称

   Product ID(产品ID): 你产品的唯一id。通常格式是 com.xx.yy,但它可以是任何形式,不要求以程序的App ID作为前缀。

   Add Language: 添加产品名称与描述语言;

   Price Tier:选择价格,这里你选择价格后,会出现如上图最下方的价格对照表

   Screenshot(截屏): 展示你产品的截屏。(这个直接无视,测试App务必要管这个的)


Product ID(产品ID)可以创建多个,比如我想游戏中分为0.99$ 、1.99$等道具那就创建对应多个产品ID

  我们填写好了”Reference Name“与”Product ID“以及”Price Tier“后,点击”Add Language“选项然后出现如下界面:

上图中的选项:

Language:语言

      Displayed Name(显示名称): 用户看到的产品名称。

      Description(描述): 对产品进行描述。

Ok,一路 Save保存回到”Manage In-App Purchases“界面中会看到我们新建的产品(道具)如下:

大家可以看到新建的产品(道具)ID:这里Himi创建的产品ID是com.himi.wahaha ,这里要记住这个产品ID哦~

第二步:申请测试账号,利用沙盒测试模拟AppStore购买道具流程!

  回到itunesconnect主页中,选择“Manage Users”然后选择“Test User”,然后出现的界面如下图:

这里Himi已经创建了两个测试账号了,点击界面中的 “Add New User”进行创建即可;记住账号和密码哈,记不住就删掉重新建 娃哈哈~(切记:不能用于真正的AppStore中使用此账号,不仅不能用,而且一旦AppStore发现后果你懂得~)

第三步:在项目中申请购买产品代码以及监听;

这里关于购买的代码部分呢,我都有备注的,Himi这里就不详细讲解了,Himi只是在代码后介绍几点值得注意的地方:

这里Himi是新建的一个Cocos2d的项目,然后给出HelloWorldLayer.h以及HelloWorldLayer.m的全部代码,所有购买代码也全在里面也对应有Himi的注释!

     HelloWorldLayer.h

[cpp] view plaincopyprint?
  1. //
  2. //  HelloWorldLayer.h
  3. //  buytest
  4. //
  5. //  Created by 华明 李 on 11-10-29.
  6. //  Copyright Himi 2011年. All rights reserved.
  7. //
  8. // When you import this file, you import all the cocos2d classes
  9. #import "cocos2d.h"
  10. #import <UIKit/UIKit.h>
  11. #import <StoreKit/StoreKit.h>
  12. enum{
  13. IAP0p99=10,
  14. IAP1p99,
  15. IAP4p99,
  16. IAP9p99,
  17. IAP24p99,
  18. }buyCoinsTag;
  19. @interface HelloWorldLayer : CCLayer<SKProductsRequestDelegate,SKPaymentTransactionObserver>
  20. {
  21. int buyType;
  22. }
  23. +(CCScene *) scene;
  24. - (void) requestProUpgradeProductData;
  25. -(void)RequestProductData;
  26. -(bool)CanMakePay;
  27. -(void)buy:(int)type;
  28. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
  29. -(void) PurchasedTransaction: (SKPaymentTransaction *)transaction;
  30. - (void) completeTransaction: (SKPaymentTransaction *)transaction;
  31. - (void) failedTransaction: (SKPaymentTransaction *)transaction;
  32. -(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;
  33. -(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;
  34. - (void) restoreTransaction: (SKPaymentTransaction *)transaction;
  35. -(void)provideContent:(NSString *)product;
  36. -(void)recordTransaction:(NSString *)product;
  37. @end
//
//  HelloWorldLayer.h
//  buytest
//
//  Created by 华明 李 on 11-10-29.
//  Copyright Himi 2011年. All rights reserved.
//// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import <UIKit/UIKit.h>#import <StoreKit/StoreKit.h>
enum{IAP0p99=10,IAP1p99,IAP4p99,IAP9p99, IAP24p99,
}buyCoinsTag; @interface HelloWorldLayer : CCLayer<SKProductsRequestDelegate,SKPaymentTransactionObserver>
{int buyType;
}+(CCScene *) scene;
- (void) requestProUpgradeProductData;
-(void)RequestProductData;
-(bool)CanMakePay;
-(void)buy:(int)type;
- (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)provideContent:(NSString *)product;
-(void)recordTransaction:(NSString *)product;
@end

HelloWorldLayer.m

[cpp] view plaincopyprint?
  1. //
  2. //  IapLayer.m
  3. //
  4. //  Created by Himi on 11-5-25.
  5. //  Copyright 2011年 李华明 . All rights reserved.
  6. //
  7. #import "HelloWorldLayer.h"
  8. #define ProductID_IAP0p99 @"com.buytest.one"//$0.99
  9. #define ProductID_IAP1p99 @"com.buytest.two" //$1.99
  10. #define ProductID_IAP4p99 @"com.buytest.three" //$4.99
  11. #define ProductID_IAP9p99 @"com.buytest.four" //$19.99
  12. #define ProductID_IAP24p99 @"com.buytest.five" //$24.99
  13. @implementation HelloWorldLayer
  14. +(CCScene *) scene
  15. {
  16. CCScene *scene = [CCScene node];
  17. HelloWorldLayer *layer = [HelloWorldLayer node];
  18. [scene addChild: layer];
  19. return scene;
  20. }
  21. -(id)init
  22. {
  23. if ((self = [super init])) {
  24. CGSize size = [[CCDirector sharedDirector] winSize];
  25. CCSprite *iap_bg  = [CCSprite spriteWithFile:@"Icon.png"];
  26. [iap_bg setPosition:ccp(size.width/2,size.height/2)];
  27. [self addChild:iap_bg z:0];
  28. //---------------------
  29. //----监听购买结果
  30. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  31. //申请购买
  32. /*
  33. enum{
  34. IAP0p99=10,
  35. IAP1p99,
  36. IAP4p99,
  37. IAP9p99,
  38. IAP24p99,
  39. }buyCoinsTag;
  40. */
  41. [self buy:IAP24p99];
  42. }
  43. return self;
  44. }
  45. -(void)buy:(int)type
  46. {
  47. buyType = type;
  48. if ([SKPaymentQueue canMakePayments]) {
  49. //[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  50. [self RequestProductData];
  51. CCLOG(@"允许程序内付费购买");
  52. }
  53. else
  54. {
  55. CCLOG(@"不允许程序内付费购买");
  56. UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert"
  57. message:@"You can‘t purchase in app store(Himi说你没允许应用程序内购买)"
  58. delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)",nil) otherButtonTitles:nil];
  59. [alerView show];
  60. [alerView release];
  61. }
  62. }
  63. -(bool)CanMakePay
  64. {
  65. return [SKPaymentQueue canMakePayments];
  66. }
  67. -(void)RequestProductData
  68. {
  69. CCLOG(@"---------请求对应的产品信息------------");
  70. NSArray *product = nil;
  71. switch (buyType) {
  72. case IAP0p99:
  73. product=[[NSArray alloc] initWithObjects:ProductID_IAP0p99,nil];
  74. break;
  75. case IAP1p99:
  76. product=[[NSArray alloc] initWithObjects:ProductID_IAP1p99,nil];
  77. break;
  78. case IAP4p99:
  79. product=[[NSArray alloc] initWithObjects:ProductID_IAP4p99,nil];
  80. break;
  81. case IAP9p99:
  82. product=[[NSArray alloc] initWithObjects:ProductID_IAP9p99,nil];
  83. break;
  84. case IAP24p99:
  85. product=[[NSArray alloc] initWithObjects:ProductID_IAP24p99,nil];
  86. break;
  87. default:
  88. break;
  89. }
  90. NSSet *nsset = [NSSet setWithArray:product];
  91. SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
  92. request.delegate=self;
  93. [request start];
  94. [product release];
  95. }
  96. //<SKProductsRequestDelegate> 请求协议
  97. //收到的产品信息
  98. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
  99. NSLog(@"-----------收到产品反馈信息--------------");
  100. NSArray *myProduct = response.products;
  101. NSLog(@"产品Product ID:%@",response.invalidProductIdentifiers);
  102. NSLog(@"产品付费数量: %d", [myProduct count]);
  103. // populate UI
  104. for(SKProduct *product in myProduct){
  105. NSLog(@"product info");
  106. NSLog(@"SKProduct 描述信息%@", [product description]);
  107. NSLog(@"产品标题 %@" , product.localizedTitle);
  108. NSLog(@"产品描述信息: %@" , product.localizedDescription);
  109. NSLog(@"价格: %@" , product.price);
  110. NSLog(@"Product id: %@" , product.productIdentifier);
  111. }
  112. SKPayment *payment = nil;
  113. switch (buyType) {
  114. case IAP0p99:
  115. payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP0p99];    //支付$0.99
  116. break;
  117. case IAP1p99:
  118. payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP1p99];    //支付$1.99
  119. break;
  120. case IAP4p99:
  121. payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP4p99];    //支付$9.99
  122. break;
  123. case IAP9p99:
  124. payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP9p99];    //支付$19.99
  125. break;
  126. case IAP24p99:
  127. payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP24p99];    //支付$29.99
  128. break;
  129. default:
  130. break;
  131. }
  132. CCLOG(@"---------发送购买请求------------");
  133. [[SKPaymentQueue defaultQueue] addPayment:payment];
  134. [request autorelease];
  135. }
  136. - (void)requestProUpgradeProductData
  137. {
  138. CCLOG(@"------请求升级数据---------");
  139. NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];
  140. SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
  141. productsRequest.delegate = self;
  142. [productsRequest start];
  143. }
  144. //弹出错误信息
  145. - (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
  146. CCLOG(@"-------弹出错误信息----------");
  147. UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]
  148. delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
  149. [alerView show];
  150. [alerView release];
  151. }
  152. -(void) requestDidFinish:(SKRequest *)request
  153. {
  154. NSLog(@"----------反馈信息结束--------------");
  155. }
  156. -(void) PurchasedTransaction: (SKPaymentTransaction *)transaction{
  157. CCLOG(@"-----PurchasedTransaction----");
  158. NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
  159. [self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
  160. [transactions release];
  161. }
  162. //<SKPaymentTransactionObserver> 千万不要忘记绑定,代码如下:
  163. //----监听购买结果
  164. //[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  165. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易结果
  166. {
  167. CCLOG(@"-----paymentQueue--------");
  168. for (SKPaymentTransaction *transaction in transactions)
  169. {
  170. switch (transaction.transactionState)
  171. {
  172. case SKPaymentTransactionStatePurchased://交易完成
  173. [self completeTransaction:transaction];
  174. CCLOG(@"-----交易完成 --------");
  175. CCLOG(@"不允许程序内付费购买");
  176. UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert"
  177. message:@"Himi说你购买成功啦~娃哈哈"
  178. delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)",nil) otherButtonTitles:nil];
  179. [alerView show];
  180. [alerView release];
  181. break;
  182. case SKPaymentTransactionStateFailed://交易失败
  183. [self failedTransaction:transaction];
  184. CCLOG(@"-----交易失败 --------");
  185. UIAlertView *alerView2 =  [[UIAlertView alloc] initWithTitle:@"Alert"
  186. message:@"Himi说你购买失败,请重新尝试购买~"
  187. delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)",nil) otherButtonTitles:nil];
  188. [alerView2 show];
  189. [alerView2 release];
  190. break;
  191. case SKPaymentTransactionStateRestored://已经购买过该商品
  192. [self restoreTransaction:transaction];
  193. CCLOG(@"-----已经购买过该商品 --------");
  194. case SKPaymentTransactionStatePurchasing:      //商品添加进列表
  195. CCLOG(@"-----商品添加进列表 --------");
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. }
  202. - (void) completeTransaction: (SKPaymentTransaction *)transaction
  203. {
  204. CCLOG(@"-----completeTransaction--------");
  205. // Your application should implement these two methods.
  206. NSString *product = transaction.payment.productIdentifier;
  207. if ([product length] > 0) {
  208. NSArray *tt = [product componentsSeparatedByString:@"."];
  209. NSString *bookid = [tt lastObject];
  210. if ([bookid length] > 0) {
  211. [self recordTransaction:bookid];
  212. [self provideContent:bookid];
  213. }
  214. }
  215. // Remove the transaction from the payment queue.
  216. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  217. }
  218. //记录交易
  219. -(void)recordTransaction:(NSString *)product{
  220. CCLOG(@"-----记录交易--------");
  221. }
  222. //处理下载内容
  223. -(void)provideContent:(NSString *)product{
  224. CCLOG(@"-----下载--------");
  225. }
  226. - (void) failedTransaction: (SKPaymentTransaction *)transaction{
  227. NSLog(@"失败");
  228. if (transaction.error.code != SKErrorPaymentCancelled)
  229. {
  230. }
  231. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  232. }
  233. -(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction{
  234. }
  235. - (void) restoreTransaction: (SKPaymentTransaction *)transaction
  236. {
  237. NSLog(@" 交易恢复处理");
  238. }
  239. -(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{
  240. CCLOG(@"-------paymentQueue----");
  241. }
  242. #pragma mark connection delegate
  243. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  244. {
  245. NSLog(@"%@",  [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
  246. }
  247. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
  248. }
  249. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  250. switch([(NSHTTPURLResponse *)response statusCode]) {
  251. case 200:
  252. case 206:
  253. break;
  254. case 304:
  255. break;
  256. case 400:
  257. break;
  258. case 404:
  259. break;
  260. case 416:
  261. break;
  262. case 403:
  263. break;
  264. case 401:
  265. case 500:
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  272. NSLog(@"test");
  273. }
  274. -(void)dealloc
  275. {
  276. [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];//解除监听
  277. [super dealloc];
  278. }
  279. @end
//
//  IapLayer.m
//
//  Created by Himi on 11-5-25.
//  Copyright 2011年 李华明 . All rights reserved.
//  #import "HelloWorldLayer.h"
#define ProductID_IAP0p99 @"com.buytest.one"//$0.99
#define ProductID_IAP1p99 @"com.buytest.two" //$1.99
#define ProductID_IAP4p99 @"com.buytest.three" //$4.99
#define ProductID_IAP9p99 @"com.buytest.four" //$19.99
#define ProductID_IAP24p99 @"com.buytest.five" //$24.99    @implementation HelloWorldLayer
+(CCScene *) scene
{  CCScene *scene = [CCScene node];  HelloWorldLayer *layer = [HelloWorldLayer node];  [scene addChild: layer];  return scene;
}
-(id)init
{  if ((self = [super init])) {  CGSize size = [[CCDirector sharedDirector] winSize];  CCSprite *iap_bg  = [CCSprite spriteWithFile:@"Icon.png"];    [iap_bg setPosition:ccp(size.width/2,size.height/2)];  [self addChild:iap_bg z:0];  //---------------------  //----监听购买结果  [[SKPaymentQueue defaultQueue] addTransactionObserver:self];  //申请购买  /* enum{ IAP0p99=10, IAP1p99, IAP4p99, IAP9p99, IAP24p99, }buyCoinsTag; */  [self buy:IAP24p99];  }  return self;
}  -(void)buy:(int)type
{   buyType = type;    if ([SKPaymentQueue canMakePayments]) {  //[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];  [self RequestProductData];    CCLOG(@"允许程序内付费购买");  }  else  {  CCLOG(@"不允许程序内付费购买");   UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert"   message:@"You can‘t purchase in app store(Himi说你没允许应用程序内购买)"                                                          delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)",nil) otherButtonTitles:nil];  [alerView show];  [alerView release];  }
}  -(bool)CanMakePay
{  return [SKPaymentQueue canMakePayments];
}  -(void)RequestProductData
{  CCLOG(@"---------请求对应的产品信息------------");  NSArray *product = nil;  switch (buyType) {  case IAP0p99:  product=[[NSArray alloc] initWithObjects:ProductID_IAP0p99,nil];  break;  case IAP1p99:  product=[[NSArray alloc] initWithObjects:ProductID_IAP1p99,nil];  break;  case IAP4p99:  product=[[NSArray alloc] initWithObjects:ProductID_IAP4p99,nil];  break;  case IAP9p99:  product=[[NSArray alloc] initWithObjects:ProductID_IAP9p99,nil];  break;  case IAP24p99:  product=[[NSArray alloc] initWithObjects:ProductID_IAP24p99,nil];  break;  default:  break;  }  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   for(SKProduct *product in myProduct){  NSLog(@"product info");  NSLog(@"SKProduct 描述信息%@", [product description]);     NSLog(@"产品标题 %@" , product.localizedTitle);  NSLog(@"产品描述信息: %@" , product.localizedDescription);  NSLog(@"价格: %@" , product.price);  NSLog(@"Product id: %@" , product.productIdentifier);   }   SKPayment *payment = nil;   switch (buyType) {  case IAP0p99:  payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP0p99];    //支付$0.99  break;  case IAP1p99:  payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP1p99];    //支付$1.99  break;  case IAP4p99:  payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP4p99];    //支付$9.99  break;  case IAP9p99:  payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP9p99];    //支付$19.99  break;  case IAP24p99:  payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP24p99];    //支付$29.99  break;  default:  break;  }  CCLOG(@"---------发送购买请求------------");  [[SKPaymentQueue defaultQueue] addPayment:payment];    [request autorelease];   }
- (void)requestProUpgradeProductData
{  CCLOG(@"------请求升级数据---------");  NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];  SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];  productsRequest.delegate = self;  [productsRequest start];   }
//弹出错误信息
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{  CCLOG(@"-------弹出错误信息----------");  UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]  delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];  [alerView show];  [alerView release];
}  -(void) requestDidFinish:(SKRequest *)request
{  NSLog(@"----------反馈信息结束--------------");  }  -(void) PurchasedTransaction: (SKPaymentTransaction *)transaction{  CCLOG(@"-----PurchasedTransaction----");  NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];  [self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];  [transactions release];
}   //<SKPaymentTransactionObserver> 千万不要忘记绑定,代码如下:
//----监听购买结果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];  - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易结果
{  CCLOG(@"-----paymentQueue--------");  for (SKPaymentTransaction *transaction in transactions)  {  switch (transaction.transactionState)  {   case SKPaymentTransactionStatePurchased://交易完成   [self completeTransaction:transaction];  CCLOG(@"-----交易完成 --------");  CCLOG(@"不允许程序内付费购买");   UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert"   message:@"Himi说你购买成功啦~娃哈哈"                                                        delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)",nil) otherButtonTitles:nil];  [alerView show];  [alerView release];   break;   case SKPaymentTransactionStateFailed://交易失败   [self failedTransaction:transaction];  CCLOG(@"-----交易失败 --------");  UIAlertView *alerView2 =  [[UIAlertView alloc] initWithTitle:@"Alert"   message:@"Himi说你购买失败,请重新尝试购买~"                                                        delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)",nil) otherButtonTitles:nil];  [alerView2 show];  [alerView2 release];  break;   case SKPaymentTransactionStateRestored://已经购买过该商品   [self restoreTransaction:transaction];  CCLOG(@"-----已经购买过该商品 --------");  case SKPaymentTransactionStatePurchasing:      //商品添加进列表  CCLOG(@"-----商品添加进列表 --------");  break;  default:  break;  }  }
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction  {  CCLOG(@"-----completeTransaction--------");  // Your application should implement these two methods.  NSString *product = transaction.payment.productIdentifier;  if ([product length] > 0) {  NSArray *tt = [product componentsSeparatedByString:@"."];  NSString *bookid = [tt lastObject];  if ([bookid length] > 0) {  [self recordTransaction:bookid];  [self provideContent:bookid];  }  }  // Remove the transaction from the payment queue.  [[SKPaymentQueue defaultQueue] finishTransaction: transaction];  }  //记录交易
-(void)recordTransaction:(NSString *)product{  CCLOG(@"-----记录交易--------");
}  //处理下载内容
-(void)provideContent:(NSString *)product{  CCLOG(@"-----下载--------");
}  - (void) failedTransaction: (SKPaymentTransaction *)transaction{  NSLog(@"失败");  if (transaction.error.code != SKErrorPaymentCancelled)  {  }  [[SKPaymentQueue defaultQueue] finishTransaction: transaction];  }
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction{  }  - (void) restoreTransaction: (SKPaymentTransaction *)transaction  {  NSLog(@" 交易恢复处理");  }  -(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{  CCLOG(@"-------paymentQueue----");
}  #pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{  NSLog(@"%@",  [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{  }  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{  switch([(NSHTTPURLResponse *)response statusCode]) {  case 200:  case 206:  break;  case 304:   break;  case 400:   break;    case 404:  break;  case 416:  break;  case 403:  break;  case 401:  case 500:  break;  default:  break;  }
}  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  NSLog(@"test");
}  -(void)dealloc
{  [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];//解除监听[super dealloc];
}
@end

代码注释的相当清楚了,没有什么可解释的,这里说几点值得注意的地方:

1.添加对应对应代码时不要忘记,添加框架 StoreKit.framework,如何添加框架请看我的博文【iOS-Cocos2d游戏开发之十四】音频/音效/视频播放(利用Cocos2D-iPhone-Extensions嵌入Cocos2d进行视频播放!)!

2. 越狱机器无法沙盒测试!模拟器的话,Himi用4.3模拟器不可以,因为提示没有开启程序内付费- -(我都没看到模拟器有store的选项,so~);但是使用iOS5的模拟器可以测试沙盒,但是执行的顺序会有些问题,但是还没真机的童鞋可以使用,建议一切以真机实测为准

3. 千万不要忘记在iTunesConnect中创建App Bundle ID一定要跟你的项目中的info.plist中的Bundle ID保证一致!!!!

4. 以上代码中你需要修改的就是我在HelloWorldLayer.m类中的宏定义的Product ID(产品ID),例如Himi刚才新建了一个产品ID是“com.himi.wahaha"

然后我运行项目截图如下以及运行控制台打印的信息如下:

点击Buy之后运行截图以及打印信息:

输入测试账号密码后以及打印信息:

这里Himi最后一张截图是没有购买成功,这里Himi是故意截图出来的,原因就是想告诉童鞋们:

 如果你的产品信息能够正常得到,但是始终无法成功的话,不要着急,因为你的产品要进入iTunes Connect,并且Apple准备好沙箱环境需要一些时间。Himi之前遇到过,然后在过了段时间后我没有修改任何一行代码,但产品ID变为有效并能成功购买。=。 =郁闷ing~~ 其实要使产品发布到Apple的网络系统是需要一段时间的,so~这里别太着急!

           越狱机器无法正常测试沙盒的喔~

顺便提示一下:Bundle ID 尽可能与开发者证书的app ID 一致。

好了,写了这么多了,咳咳、Himi继续忙了,做iOS的童鞋们我想此篇将成为你必须收藏的一篇哦~嘿嘿!

iOS应用内付费详解相关推荐

  1. iOS 内购详解-代码篇

    内购项目-代码篇 一.分步骤说明 1.获取商品列表 2.苹果服务器返回的可购买商品 3.下单购买商品 4.购买队列状态变化,判断购买状态是否成功 5.交易验证 6.拿到的收据信息是,此App所有购买的 ...

  2. iOS开发证件要点详解

    iOS开发证书要点详解 引言 关于开发证书配置(Certificates&Identifiers&Provisioning Profiles),相信做iOS开发的同学没少被折腾.对于一 ...

  3. iOS中ImageIO框架详解与应用分析

    2019独角兽企业重金招聘Python工程师标准>>> iOS中ImageIO框架详解与应用分析 一.引言 ImageIO框架提供了读取与写入图片数据的基本方法,使用它可以直接获取到 ...

  4. iOS动画-CAAnimation使用详解

    理解了隐式动画后,显式动画就更加通俗易懂了.区别于隐式动画的特点,显式动画就是需要我们明确指定类型.时间等参数来实现效果的动画.除此之外,我们也可以创建非线性动画,比如沿着任意一条曲线运动等: 我们平 ...

  5. Cisco ××× 完全配置指南-连载-IOS ×××阶段2连接详解

    Cisco ××× 完全配置指南-连载-IOS ×××阶段2连接详解 详细配置见附件 转载于:https://blog.51cto.com/xuanbo/151764

  6. Cocos2d-x使用iOS游戏内付费IAP(C++篇)

    source file url: http://www.tairan.com/archives/5515 Cocos2d-x使用iOS游戏内付费IAP(C++篇) 前期准备 设备与账号 在开始编码之前 ...

  7. IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    转:http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINaviga ...

  8. iOS应用内付费(IAP)开发步骤列表

    iOS应用内付费(IAP)开发步骤列表 前两天和服务端同事一起,完成了应用内付费(以下简称IAP, In app purchase)的开发工作.步骤繁多,在此把开发步骤列表整理如下.因为只是步骤列表, ...

  9. iOS里面MVC模式详解

    iOS里面MVC模式详解 MVC是IOS里面也是很多程序设计里面的一种设计模式,M是model,V是view,C是controller.MVC模式在ios开发里面可谓是用得淋漓尽致. 以下是对斯坦福大 ...

最新文章

  1. 自动机器学习(AutoML)
  2. SpringBoot第二十五篇:2小时学会springboot
  3. 工业总线通信与OSI七层模型
  4. mysql命令去重_MySQL去重的方法整理
  5. python 把中文句子分割成一个一个的字(英文适用)
  6. linux 打包库文件,Linux的文件的打包(tar方法)
  7. jsp后台批量导入excel表格数据到mysql中_运用java解析excel表,拿到表中的数据并批量插入数据库...
  8. Nginx负载均衡与反向代理——基础功能
  9. 微型计算机系统评课,微机课评课稿.doc
  10. gre作文3.5什么水平计算机专业,gre作文3.5分如何达到
  11. MongoDB创建数据库
  12. 需求分析师如何做好非功能性需求
  13. 卫生事业管理学重点 题库含答案(终极完全版)
  14. HDU - 3966(树链剖分)
  15. 为什么现在的智能手机,都被设计成不可更换电池?
  16. C语言100个囚犯和灯泡,一百个囚犯和一个灯泡
  17. python语言必刷题——BMI值的计算
  18. Linux下批量把GDK编码的文章转化为UTF-8编码的文章以及“iconv: 未知 xxx 处的非法输入序列”错误处理
  19. 一文看懂:NAS网络存储与SAN、DAS的区别
  20. 用php做一个网站,如何用PHP开发一个完整的网站

热门文章

  1. 数据类型(简单数据类型、简单数据类型传参、复杂数据类型传参)
  2. 固定定位(HTML、CSS)
  3. matlab 创建批量文件夹_学会这12个批量操作,从此告别加班!
  4. CentOS 7 多网卡绑定
  5. php 取oracle图片,一个php导出oracle库的php代码
  6. 永川机器人五小区_永川清掏化粪池报价---蚂众蚁
  7. 算法练习——聪明的情侣
  8. 单因子测试用什么软件检查,如何进行单元测试?
  9. 网络适配器本地连接没有有效ip地址配置错误的解决办法
  10. kittito_rosbag入坑教程