由于没有人回答这个问题,我会......即使只是为了帮助以后查看这个的人。
请承认,我还没有使用应用内购买.. 但我是一位经验丰富的 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...