如何提供storekit内容?

时间:2009-10-25 21:24:17

标签: iphone macos iphone-sdk-3.0 storekit

对于我的应用程序,我觉得它会要求确认和帐户进行应用程序内购买,但我不知道如何在购买完成后启用该项目。

SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.silver.tapp.page2"];

[[SKPaymentQueue defaultQueue] addPayment:payment];

这就是我到目前为止所拥有的

[[p2Controller tabBarItem] setEnabled:TRUE];

这是我想要执行的代码

1 个答案:

答案 0 :(得分:20)

首先,您应该回过头来查看Apple提供的In App购买documentation - 他们会详细说明这一点,了解他们所做的工作是有用的。

您缺少的是SKPaymentTransactionObserver - 您有责任实现此观察者,您将其添加为SKPaymentTransactionQueue的观察者。 Apple建议您将应用启动时的观察者添加到默认队列,以便能够观察应用运行时发生的所有事务。

基本上,您需要编写自己的类来实现SKPaymentTransactionObserver协议。这个课程的作用是观察当iTunes商店处理付款时从付款队列中捕获回调,让你抓住成功和失败事件。

以下是付款观察员的骨架:

PaymentObserver.h

#import <StoreKit/StoreKit.h>

@interface PaymentObserver : NSObject <SKPaymentTransactionObserver> {  
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction;
- (void) restoreTransaction: (SKPaymentTransaction *)transaction;
- (void) failedTransaction: (SKPaymentTransaction *)transaction;

@end

PaymentObserver.m

#import "PaymentObserver.h"
@implementation PaymentObserver

- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions
{
  // handle payment cancellation
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
  // handle the payment transaction actions for each state
  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];        
      default:
        break;
    }
  }
}



- (void) completeTransaction: (SKPaymentTransaction *)transaction;
{
  // Record the transaction
  //...

  // Do whatever you need to do to provide the service/subscription purchased
  //...

  // Remove the transaction from the payment queue.
  [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
  // Record the transaction
  //...

  // Do whatever you need to do to provide the service/subscription purchased
  //...

  [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{  
  if (transaction.error.code != SKErrorPaymentCancelled)
  {
    // Optionally, display an error here.    
  }
  [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

@end

实施PaymentObserver后,您需要将其作为事务观察者添加到默认支付队列中。

// Done at app launch...
PaymentObserver *paymentObserver = [[PaymentObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:paymentObserver];