How to provide storekit content?

For my app, I have enough to ask for confirmation and an account for an in-app purchase, but I don't know how to enable the item after completing the purchase.

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

      

[[SKPaymentQueue defaultQueue] addPayment: payment];

That's what I have so far

[[p2Controller tabBarItem] setEnabled:TRUE];

      

this is the code i would like to execute

+2


source to share


1 answer


You should first go back and look at the In App purchase documentation that Apple provides - they detail this, and it's helpful to understand what's going on on their side.

What you are missing is SKPaymentTransactionObserver - it is your responsibility to inject this observer, which you add as the SKPaymentTransactionQueue observer. Apple recommends adding an app startup watcher to the default queue so that it can watch all transactions that occur while your app is running.

Essentially, you need to write your own class that implements the SKPaymentTransactionObserver protocol. What this class does is watch callbacks from the payment queue as the iTunes store processes the payment and allows you to track success and failure events.

Here's a skeleton for the Payment Explorer:

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

      

After you have implemented PaymentObserver, you need to add the instance to the default payment queue as a transaction watcher.

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

      

+19


source







All Articles