,我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...
如果您有'NSUserDefaults'到位的代碼,你有什麼不確定當購買成功完成後,只需保存的硬幣?價值,然後在必要時檢索它。 –
感謝您的迴應。我只是不確定如何開始使用IAP。有很多教程,但沒有一個解決IAP消耗品和NSUser默認等...尋找第一步類型的東西... – Herbie999