Article

Setting Up the Transaction Observer and Payment Queue

Enable your app to receive and handle transactions by adding an observer.

Overview

To process transactions in your app, you must create and add an observer to the payment queue. The observer object responds to new transactions and synchronizes the queue of pending transactions with the App Store, and the payment queue prompts the user to authorize payment. Your app should add the transaction observer at app launch to ensure that your app will receive payment queue notifications as soon as possible.

Create an Observer

Create and build out a custom observer class to handle changes to the payment queue:

class StoreObserver: NSObject, SKPaymentTransactionObserver {
                ....
    //Initialize the store observer.
    override init() {
        super.init()
        //Other initialization here.
    }

    //Observe transaction updates.
    func paymentQueue(_ queue: SKPaymentQueue,updatedTransactions transactions: [SKPaymentTransaction]) {
        //Handle transaction states here.
    }
                ....
}

Create an instance of this observer class to act as the observer of changes to the payment queue:

let iapObserver = StoreObserver()

Once you've created the transaction observer, you can add it to the payment queue.

Add an Observer

StoreKit attaches your observer to the queue when your app calls:

SKPaymentQueue.default().add(iapObserver)

StoreKit can notify your SKPaymentTransactionObserver instance automatically when the content of the payment queue changes upon resuming or while running your app.

Implement the transaction observer:

import UIKit
import StoreKit

class AppDelegate: UIResponder, UIApplicationDelegate {
                ....
    // Attach an observer to the payment queue.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        SKPaymentQueue.default().add(iapObserver)
        return true
    }

    // Called when the application is about to terminate.
    func applicationWillTerminate(_ application: UIApplication) {
        // Remove the observer.
        SKPaymentQueue.default().remove(iapObserver)
    }
                ....
}

It is important to add the observer at launch, in application(_:didFinishLaunchingWithOptions:), to ensure that it persists during all launches of your app and receives all payment queue notifications.

The observer must be persistent so it is not deallocated when the app is sent to the background. Only a persistent observer can receive transactions that may occur while your app is in the background, such as a renewal transaction for an auto-renewable subscription.

Remove an Observer

StoreKit removes an observer from the payment queue when your app calls:

SKPaymentQueue.default().remove(iapObserver)

StoreKit may attempt to notify the above observer if it was not removed from the payment queue, thus causing your app to crash, as the observer no longer exists. Keep in mind that if you remove the observer from the queue, your app may fail to observe changes made to the queue or deliver content as needed.

See Also

Essentials

Offering, Completing, and Restoring In-App Purchases

Fetch, complete, and restore transactions in your app.

class SKPaymentQueue

A queue of payment transactions to be processed by the App Store.

protocol SKPaymentQueueDelegate

The protocol implemented to provide information needed to complete transactions.

protocol SKPaymentTransactionObserver

A set of methods that process transactions, unlock purchased functionality, and continue promoted in-app purchases.