誰もこれに答えなかったので、後でこれを見る人を助けるためだけにでも..
アプリ内購入はまだ使用していないことをご了承ください..しかし、私は経験豊富な obj-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...