如何使用NSUserDefaults而不是我自己的服务器在应用程序购买中添加易耗品?

我是新的,所以我需要简单的答案,如果可能的话。 我有我的第一个应用程序刚刚完成(但它甚至没有提交到商店),我想添加在应用程序购买。 我的应用程序是一个游戏,用户从虚拟货币“硬币”开始。 我想为他们添加一个IAP,以便能够在三到五个不同的价位上购买更多的硬币。

我看过很多教程,看起来信息混杂。 任何人都可以帮助我如何在这里继续下去,对于一个非常新的开发人员来说,这是一个现实的方式,并与我的具体情况相关。 我想免费添加IAP,但是如果有一个简单的付费解决scheme是非常值得的,那可能没关系。

我正在使用Xcode 4.3.2。 我想使用NSUserDefaults而不是我自己的服务器,我有NSUserDefaults的代码。 至less它存储了“硬币”,并具有价值的关键。

另外现在IOS 6已经不同了,我需要一个全新的Xcode版本吗?

感谢您的帮助,请原谅我在编写好问题方面的经验不足:)

由于没有人回答这个问题,所以我会。即使只是为了帮助那些后来看这个。

请确认,我还没有使用应用程序内购买呢..但我是一个经验丰富的obj-c程序员,我相信我了解了这个概念后,查看苹果文档一次…这是一个非常简单的例子,“消费品”应用内购买(*注意,如果它们是非消耗品或其他物品,则应包含更多内容):

首先,将“StoreKit”Objective-C框架包含到您的项目中。

我创build了这个简单的类“InAppPurchaser” – 请包括这个类也..

“InAppPurchaser.h”:

 #import <Foundation/Foundation.h> #import <StoreKit/StoreKit.h> @protocol InAppPurchaserDelegate <NSObject> - (void) InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID; - (void) InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID error:(NSError *)error; @end @interface InAppPurchaser : NSObject <SKPaymentTransactionObserver> @property (nonatomic, retain) id <InAppPurchaserDelegate> delegate; #pragma mark Instantiation... - (id) init; + (InAppPurchaser *) purchaser; + (InAppPurchaser *) purchaserWithDelegate:(id <InAppPurchaserDelegate>)delegate; #pragma mark Utilities... - (void) purchaseProductWithProductIdentifier:(NSString *)productID quantity:(NSInteger)quantity; - (void) restoreTransactions; @end 

“InAppPurchaser.m”:

 #import "InAppPurchaser.h" @implementation InAppPurchaser @synthesize delegate; - (id) init { if ( self = [super init] ) { [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; } // ends condition... return( self ); } // ends method... + (InAppPurchaser *) purchaser { return( [[InAppPurchaser alloc] init] ); } // ends method... + (InAppPurchaser *) purchaserWithDelegate:(id <InAppPurchaserDelegate>)delegate { InAppPurchaser *purchaser = [InAppPurchaser purchaser]; purchaser.delegate = delegate; return( purchaser ); } // ends method... #pragma mark Actions... - (void) purchaseProductWithProductIdentifier:(NSString *)productID quantity:(NSInteger)quantity { SKMutablePayment *payment = [[SKMutablePayment alloc] init]; payment.productIdentifier = productID; payment.quantity = quantity; [[SKPaymentQueue defaultQueue] addPayment:payment]; } // ends method... #pragma mark SKPaymentTransactionObserver... - (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { for ( SKPaymentTransaction * transaction in transactions ) { switch ( transaction.transactionState ) { case SKPaymentTransactionStatePurchased: [self completeTransaction:transaction]; break; case SKPaymentTransactionStateFailed: [self failedTransaction:transaction]; break; case SKPaymentTransactionStateRestored: [self restoreTransaction:transaction]; break; default: // ... break; } // ends switch statement... } // ends loop... } // ends method... - (void) completeTransaction:(SKPaymentTransaction *)transaction { if ( delegate != nil && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionSuccessfully:productID:)] ) [delegate InAppPurchaserHasCompletedTransactionSuccessfully:transaction productID:transaction.payment.productIdentifier]; [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } // ends method... - (void) restoreTransaction:(SKPaymentTransaction *)transaction { if ( delegate != nil && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionSuccessfully:productID:)] ) [delegate InAppPurchaserHasCompletedTransactionSuccessfully:transaction productID:transaction.payment.productIdentifier]; [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } // ends method... - (void) failedTransaction:(SKPaymentTransaction *)transaction { if ( delegate != nil && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionUnsuccessfully:productID:error:)] ) [delegate InAppPurchaserHasCompletedTransactionUnsuccessfully:transaction productID:transaction.payment.productIdentifier error:transaction.error]; [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; } // ends method... - (void) restoreTransactions { [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; } // ends method... @end 

用法示例:

“MyClassViewController.h”:

 #import <UIKit/UIKit.h> #import "InAppPurchaser.h" @interface MyClassViewController : UIViewController <InAppPurchaserDelegate> @end 

“MyClassViewController.m”:

 #import "MyClassViewController.h" @interface MyClassViewController () @end @implementation MyClassViewController - (void)viewDidLoad { [super viewDidLoad]; } // ends method... - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } // ends method... #pragma mark Purchasing... - (void) myPurchaseMethod { InAppPurchaser *purchaser = [InAppPurchaser purchaserWithDelegate:self]; [purchaser purchaseProductWithProductIdentifier:@"MyProduct" quantity:1]; } // ends method... - (void) InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID error:(NSError *)error { // handle success code.. IE: [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MyProduct"]; } // ends method... - (void) InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID { // handle failure code... } // ends method... @end 

这个代码没有经过testing,但它应该工作..显然,它可以改善,但它应该让你开始。

最后,你也希望在你的AppDelegate中使用它,就像从ViewController中调用它,除非不要求购买..只需调用我包含的“restoreTransactions”方法。这将恢复任何不完整由于应用或互联网连接失败或其他原因而发生的交易。

例:

在“AppDelegate.h”中:

 @interface AppDelegate : UIResponder <UIApplicationDelegate, InAppPurchaserDelegate> 

在“AppDelegate.m”中:

 - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { InAppPurchaser *purchaser = [InAppPurchaser purchaserWithDelegate:self]; [purchaser restoreTransactions]; return( YES ); } // ends method... - (void) InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID error:(NSError *)error { // handle success code.. IE: [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MyProduct"]; } // ends method... - (void) InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID { // handle failure code... } // ends method...