[英]How do I add consumable In App Purchases using NSUserDefaults and not my own server?
我是新手,所以如果可能的话,我需要简单的答案。 我的第一个应用程序即将完成(但它甚至没有提交到商店),我想添加应用程序内购买。 我的应用程序是一个游戏,用户从一些虚拟货币“硬币”开始。 我想为他们添加一个 IAP,以便他们能够以三到五个不同的价格点购买更多的代币。
我看过大量的教程,似乎信息是混合的。 任何人都可以帮助我了解如何在这里进行,以一种对非常新的开发人员来说是现实的并且与我的特定情况相关的方式。 我想免费添加 IAP,但如果有一个真正值得的简单付费解决方案,那可能没问题。
我正在使用 Xcode 4.3.2。 我想使用 NSUserDefaults 而不是我自己的服务器,并且我为 NSUserDefaults 准备了代码。 至少它存储“硬币”并具有值的键。
此外,现在 IOS 6 出来了,情况是否有所不同,我是否需要全新版本的 Xcode?
感谢您的帮助,请原谅我写好问题的经验不足:)
由于没有人回答这个问题,我会......即使只是帮助以后查看此内容的人。
请承认,我还没有使用应用内购买......但我是一名经验丰富的 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...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.