Register a transaction queue observer to get and handle transaction updates from the App Store.
Implementing an in-app purchase flow can be divided into three stages. You first retrieve the product information, then send a payment request by adding it to the payment queue. Finally, your app delivers the products for successful transactions. This article details the steps performed by your app and the App Store in the last stage, as highlighted in Figure 1.
The App Store calls the transaction queue observer after it processes the payment request. Your app then records information about the purchase for future launches, downloads the purchased content, and marks the transaction as finished.
Monitor Transactions in the Queue
The transaction queue plays a central role in letting your app communicate with the App Store through the StoreKit framework. You add work to the queue that the App Store needs to act on, such as a payment request to be processed. When the transaction’s state changes, such as when a payment request succeeds, StoreKit calls the app’s transaction queue observer. You decide which class acts as the observer. In very small apps, you could handle all the StoreKit logic in the app delegate, including observing the transaction queue. In most apps, however, you create a separate class that handles this observer logic along with the rest of your app’s store logic. The observer must conform to the
By adding an observer, your app does not need to constantly poll the status of its active transactions. Your app uses the transaction queue for payment requests, to download Apple-hosted content, and to find out that subscriptions have been renewed.
Always register a transaction queue observer as soon as your app is launched, as shown below. For more guidance, see Setting Up the Transaction Observer and Payment Queue.
Make sure that the observer is ready to handle a transaction at any time, not only after you add a transaction to the queue. For example, if a user buys something in your app right before going into a tunnel, your app may not be able to deliver the purchased content if there is no network connection. The next time your app launches, StoreKit calls your transaction queue observer again and your app should handle the transaction and deliver the purchased content. Similarly, if your app fails to mark a transaction as finished, StoreKit calls the observer every time your app launches until the transaction finishes.
payment method on your transaction queue observer. StoreKit calls this method when the status of a transaction changes, such as when a payment request has been processed. The transaction status tells you what action your app needs to perform, as described in Table 1.
Action to take in your app
Update your UI to reflect the in-progress status, and wait to be called again.
Update your UI to reflect the deferred status, and wait to be called again.
Use the value of the
Provide the purchased functionality, typically by unlocking features or delivering content.
Restore the previously purchased functionality.
Transactions in the queue can change state in any order. Your app needs to be ready to work on any active transaction at any time. Act on every transaction according to its transaction state, as in this example:
Update the App's UI to Reflect Transaction Changes
To keep your user interface up to date while waiting, the transaction queue observer can implement optional methods from the
SKPayment protocol as follows:
StoreKit calls the
paymentmethod when it removes transactions from the queue. In your implementation of this method, remove the corresponding items from your app’s UI.
Queue: removed Transactions:
StoreKit calls the
Queue Restore Completed Transactions Finished:
paymentmethods when it finishes restoring transactions, depending on whether there was an error. In your implementation of these methods, update your app’s UI to reflect the success or failure.
Queue: restore Completed Transactions Failed With Error:
For successfully processed transactions, your app should validate the receipt associated with the transaction to verify the items purchased by the user and unlock content accordingly. For more information on validating receipts server-side, see Validating Receipts with the App Store.