Enable your app to receive and handle transactions by adding an observer.
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:
Create an instance of this observer class to act as the observer of changes to the payment queue:
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:
StoreKit can notify your
SKPayment instance automatically when the content of the payment queue changes upon resuming or while running your app.
Implement the transaction observer:
It is important to add the observer at launch, in
application:, 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:
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.