2012-11-20 65 views
1

我是新來的,所以我需要簡單的答案,如果可能的話。我有我的第一個應用程序剛剛完成(但它甚至沒有提交到商店),我想添加在應用程序購買。我的應用程序是用戶使用某些虛擬貨幣「硬幣」開始的遊戲。我想爲他們添加一個IAP,以便能夠在三個甚至五個不同的價位上購買更多的硬幣。如何使用NSUserDefaults而不是我自己的服務器在應用程序採購中添加消耗品?

我見過大量的教程,它似乎信息混合。任何人都可以幫助我解決如何在這裏進行,這對於一個非常新的開發者來說是現實的,並且與我的特定案例相關。我想免費添加IAP,但如果有一個真正值得的簡單付費解決方案,那可能沒問題。

我正在使用Xcode 4.3.2。我想使用NSUserDefaults而不是我自己的服務器,並且我有用於NSUserDefaults的代碼。至少它存儲了「硬幣」並且具有價值的關鍵。

此外,現在IOS 6已經不同了,我需要一個全新版本的Xcode嗎?

感謝您的幫助,請原諒我的經驗不足。由於沒有人回答了這個寫很好的問題:)

+1

如果您有'NSUserDefaults'到位的代碼,你有什麼不確定當購買成功完成後,只需保存的硬幣?價值,然後在必要時檢索它。 –

+0

感謝您的迴應。我只是不確定如何開始使用IAP。有很多教程,但沒有一個解決IAP消耗品和NSUser默認等...尋找第一步類型的東西... – Herbie999

回答

2

,我will..even如果僅僅是爲了幫助人們誰以後查看此。

請注意,我還沒有使用應用程序內購買呢..但我是一個經驗豐富的obj-c程序員&我相信我在查看蘋果文檔之後理解了這個概念......這是一個非常簡單的例子首先,將「StoreKit」Objective-C框架包含到您的項目中。如果您的項目中包含「StoreKit」Objective-C框架,則可以使用「StoreKit」Objective-C框架。

我創造了這個名爲 「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 

此代碼並沒有進行測試,但它應該工作。顯然,它可以改進,但它應該讓你開始。

最後,你也想在你的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... 
相關問題