我是新手,所以如果可能的话,我需要简单的答案。我的第一个应用程序刚刚完成(但它甚至没有提交到商店),我想添加In App Purchases。我的应用程序是一个游戏,用户以一些虚拟货币“硬币”开始。我想为他们添加一个IAP,以便能够以三个到五个不同的价格点购买更多的硬币。
我看过很多教程,看起来信息很复杂。任何人都可以帮助我如何在这里继续,以一种对于一个非常新的开发人员而且与我的特定案例相关的方式。我想免费添加IAP,但如果有一个简单的付费解决方案真的值得,那可能没问题。
我正在使用Xcode 4.3.2。我想使用NSUserDefaults而不是我自己的服务器,我有NSUserDefaults的代码。至少它存储了“硬币”并且有一个关键值。
此外,由于IOS 6出现了不同之处,我是否需要全新版本的Xcode? p>
感谢您的帮助,请原谅我在撰写好问题方面缺乏经验:)
答案 0 :(得分:2)
由于没有人回答这个问题,我会......如果只是为了帮助那些稍后再看的人。
请注意,我还没有使用应用程序内购买..但我是一位经验丰富的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...