iOS内购In-App Purchase

内购In-App Purchase

转载自:http://blog.csdn.net/gwh111/article/details/19334259

https://developer.apple.com/in-app-purchase/In-App-Purchase-Guidelines.pdf

允许范围:
电子书或者电子相册
额外游戏关数
地图
电子杂志
数字资料

四种种类:
内容
功能
服务
子部件

五种类别
可消耗
不可消耗
自动更新
免费订阅?
不可更新

对于内容:设置一个binary,enable it 当用户购买后

https://developer.apple.com/in-app-purchase/

invalid product IDs

1.有没有enabled In-App Purchases for your App ID?
2.有没有设置为Cleared for Sale
3.有没有上传过二进制文件(然后rejected it?
4.你的project's .plist BundleID 是否和你的App ID 一致?
5.有没有安装新的provisioning profile?
6. 使用完整的product ID?
7.等待两个小时以上
8.删除旧的应用,重新安装一遍
9. 有没有越狱? (我就是越狱了-。-) 越狱的机子无法测试内购

测试购买失败?

有没有注销你设备上的app store。必须第一次就注销,否则出现什么情况

提示你已经购买了此程序内购买项目,但尚未下载.

解决办法:1.换一台设备?2.等几天



.h

  1. #import <UIKit/UIKit.h>  
  2. #import <StoreKit/StoreKit.h>  
  3. #define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification"  
  4. #define kInAppPurchaseManagerTransactionFailedNotification @"kInAppPurchaseManagerTransactionFailedNotification"  
  5. #define kInAppPurchaseManagerTransactionSucceededNotification @"kInAppPurchaseManagerTransactionSucceededNotification"  
  6.   
  7. @interface cgViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,SKProductsRequestDelegate,SKPaymentTransactionObserver>{  
  8.     SKProduct *proUpgradeProduct;  
  9.     SKProductsRequest *productsRequest;  
  10. }  
  11. - (void)loadStore;  
  12. - (BOOL)canMakePurchases;  
  13. - (void)purchaseProUpgrade;  
  14. @end  

.m

  1. #define kInAppPurchaseProUpgradeProductId @"完整的product id"  
  2. #pragma -  
  3. #pragma Public methods  
  4. //  
  5. // call this method once on startup  
  6. //  
  7. - (void)loadStore  
  8. {  
  9.     // restarts any purchases if they were interrupted last time the app was open  
  10.     [[SKPaymentQueue defaultQueue] addTransactionObserver:self];  
  11.       
  12.     // get the product description (defined in early sections)  
  13.     [self requestProUpgradeProductData];  
  14. }  
  15. //  
  16. // call this before making a purchase  
  17. //  
  18. - (BOOL)canMakePurchases  
  19. {  
  20.     NSLog(@"%d",[SKPaymentQueue canMakePayments]);  
  21.     return [SKPaymentQueue canMakePayments];  
  22. }  
  23. //  
  24. // kick off the upgrade transaction  
  25. //  
  26. - (void)purchaseProUpgrade  
  27. {  
  28.     NSLog(@"ppp");  
  29.     SKPayment *payment = [SKPayment paymentWithProductIdentifier:kInAppPurchaseProUpgradeProductId];  
  30.     [[SKPaymentQueue defaultQueue] addPayment:payment];  
  31. }  
  32. #pragma -  
  33. #pragma Purchase helpers  
  34. //  
  35. // saves a record of the transaction by storing the receipt to disk  
  36. //  
  37. - (void)recordTransaction:(SKPaymentTransaction *)transaction  
  38. {  
  39.     if ([transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId])  
  40.     {  
  41.         // save the transaction receipt to disk  
  42.         [[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];  
  43.         [[NSUserDefaults standardUserDefaults] synchronize];  
  44.     }  
  45. }  
  46. //  
  47. // enable pro features  
  48. //  
  49. - (void)provideContent:(NSString *)productId  
  50. {  
  51.     if ([productId isEqualToString:kInAppPurchaseProUpgradeProductId])  
  52.     {  
  53.         // enable the pro features  
  54.         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isProUpgradePurchased" ];  
  55.         [[NSUserDefaults standardUserDefaults] synchronize];  
  56.     }  
  57. }  
  58. //  
  59. // removes the transaction from the queue and posts a notification with the transaction result  
  60. //  
  61. - (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful  
  62. {  
  63.     // remove the transaction from the payment queue.  
  64.     [[SKPaymentQueue defaultQueue] finishTransaction:transaction];  
  65.       
  66.     NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];  
  67.     if (wasSuccessful)  
  68.     {  
  69.         // send out a notification that we’ve finished the transaction  
  70.         NSLog(@"succ");  
  71.         [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo];  
  72.     }  
  73.     else  
  74.     {  
  75.         // send out a notification for the failed transaction  
  76.         NSLog(@"fail");  
  77.         [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo];  
  78.     }  
  79. }  
  80. //  
  81. // called when the transaction was successful  
  82. //  
  83. - (void)completeTransaction:(SKPaymentTransaction *)transaction  
  84. {  
  85.     [self recordTransaction:transaction];  
  86.     [self provideContent:transaction.payment.productIdentifier];  
  87.     [self finishTransaction:transaction wasSuccessful:YES];  
  88. }  
  89. //  
  90. // called when a transaction has been restored and and successfully completed  
  91. //  
  92. - (void)restoreTransaction:(SKPaymentTransaction *)transaction  
  93. {  
  94.     [self recordTransaction:transaction.originalTransaction];  
  95.     [self provideContent:transaction.originalTransaction.payment.productIdentifier];  
  96.     [self finishTransaction:transaction wasSuccessful:YES];  
  97. }  
  98. //  
  99. // called when a transaction has failed  
  100. //  
  101. - (void)failedTransaction:(SKPaymentTransaction *)transaction  
  102. {  
  103.     if (transaction.error.code != SKErrorPaymentCancelled)  
  104.     {  
  105.         // error!  
  106.         [self finishTransaction:transaction wasSuccessful:NO];  
  107.     }  
  108.     else  
  109.     {  
  110.         // this is fine, the user just cancelled, so don’t notify  
  111.         [[SKPaymentQueue defaultQueue] finishTransaction:transaction];  
  112.     }  
  113. }  
  114. #pragma mark -  
  115. #pragma mark SKPaymentTransactionObserver methods  
  116. //  
  117. // called when the transaction status is updated  
  118. //  
  119. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions  
  120. {  
  121.     for (SKPaymentTransaction *transaction in transactions)  
  122.     {  
  123.         switch (transaction.transactionState)  
  124.         {  
  125.             case SKPaymentTransactionStatePurchased:  
  126.                 [self completeTransaction:transaction];  
  127.                 break;  
  128.             case SKPaymentTransactionStateFailed:  
  129.                 [self failedTransaction:transaction];  
  130.                 break;  
  131.             case SKPaymentTransactionStateRestored:  
  132.                 [self restoreTransaction:transaction];  
  133.                 break;  
  134.             default:  
  135.                 break;  
  136.         }  
  137.     }  
  138. }  
  139.   
  140.   
  141.   
  142.   
  143. - (void)requestProUpgradeProductData  
  144. {  
  145.     NSSet *productIdentifiers = [NSSet setWithObject:@"gwh.syzg.syzgtext" ];  
  146.     productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];  
  147.     productsRequest.delegate = self;  
  148.     [productsRequest start];  
  149.       
  150.     // we will release the request object in the delegate callback  
  151. }  
  152. #pragma mark -  
  153. #pragma mark SKProductsRequestDelegate methods  
  154. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response  
  155. {  
  156.     NSArray *products = response.products;  
  157.     proUpgradeProduct = [products count] == 1 ? [products firstObject]  : nil;  
  158.     if (proUpgradeProduct)  
  159.     {  
  160.         NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);  
  161.         NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);  
  162.         NSLog(@"Product price: %@" , proUpgradeProduct.price);  
  163.         NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);  
  164.     }  
  165.       
  166.     for (NSString *invalidProductId in response.invalidProductIdentifiers)  
  167.     {  
  168.         NSLog(@"Invalid product id: %@" , invalidProductId);  
  169.     }  
  170.       
  171.     // finally release the reqest we alloc/init’ed in requestProUpgradeProductData  
  172.       
  173.     [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];  
  174. }  

demo:http://download.csdn.net/detail/gwh111/6932009

Important: To associate In-App Purchase products with the release of your app, make sure its status is “Prepare for Upload.”

https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnectInAppPurchase_Guide/Chapters/SubmittingInAppPurchases.html#//apple_ref/doc/uid/TP40013727-CH5-SW1

更多内容参考:

http://www.cocoachina.com/bbs/read.php?tid=69165&keyword=IAP%3C/p%3E

http://www.cocoachina.com/gamedev/misc/2012/0409/4129.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值