1

我是新手,所以如果可能的话,我需要简单的答案。我的第一个应用程序即将完成(但它甚至没有提交到商店),我想添加应用程序内购买。我的应用程序是一个用户从一些虚拟货币“硬币”开始的游戏。我想为他们添加一个 IAP,以便他们能够以三到五个不同的价格点购买更多的硬币。

我看过大量的教程,似乎信息参差不齐。任何人都可以帮助我了解如何在这里进行,对于一个非常新的开发人员来说是现实的并且与我的特定案例相关。我想免费添加 IAP,但如果有一个真正值得的简单付费解决方案,那可能没问题。

我正在使用 Xcode 4.3.2。我想使用 NSUserDefaults 而不是我自己的服务器,并且我为 NSUserDefaults 准备了代码。至少它存储“硬币”并具有价值的关键。

此外,现在 IOS 6 发布后情况有所不同,我需要全新版本的 Xcode 吗?

感谢您的帮助,请原谅我没有写好问题的经验:)

4

1 回答 1

4

由于没有人回答这个问题,我会......即使只是为了帮助以后查看这个的人。

请承认,我还没有使用应用内购买.. 但我是一位经验丰富的 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...
于 2013-01-13T21:19:22.130 回答