Skip to content
Samuel Spencer edited this page Jan 22, 2021 · 1 revision

Restore Previous Purchases

According to Apple - Restoring Purchased Products:

In most cases, all your app needs to do is refresh its receipt and deliver the products in its receipt. The refreshed receipt contains a record of the user’s purchases in this app, on this device or any other device.

Restoring completed transactions creates a new transaction for every completed transaction the user made, essentially replaying history for your transaction queue observer.

See the Receipt Verification section below for how to restore previous purchases using the receipt.

This section shows how to restore completed transactions with the restorePurchases method instead. When successful, the method returns all non-consumable purchases, as well as all auto-renewable subscription purchases, regardless of whether they are expired or not.

  • Atomic: to be used when the content is delivered immediately.
SwiftyStoreKit.restorePurchases(atomically: true) { results in
    if results.restoreFailedPurchases.count > 0 {
        print("Restore Failed: \(results.restoreFailedPurchases)")
    } else if results.restoredPurchases.count > 0 {
        print("Restore Success: \(results.restoredPurchases)")
    } else {
        print("Nothing to Restore")
    }
}
  • Non-Atomic: to be used when the content is delivered by the server.
SwiftyStoreKit.restorePurchases(atomically: false) { results in
    if results.restoreFailedPurchases.count > 0 {
        print("Restore Failed: \(results.restoreFailedPurchases)")
    } else if results.restoredPurchases.count > 0 {
        for purchase in results.restoredPurchases {
            // fetch content from your server, then:
            if purchase.needsFinishTransaction {
                SwiftyStoreKit.finishTransaction(purchase.transaction)
            }
        }
        print("Restore Success: \(results.restoredPurchases)")
    } else {
        print("Nothing to Restore")
    }
}

What does atomic / non-atomic mean?

When you purchase a product the following things happen:

  • A payment is added to the payment queue for your IAP.
  • When the payment has been processed with Apple, the payment queue is updated so that the appropriate transaction can be handled.
  • If the transaction state is purchased or restored, the app can unlock the functionality purchased by the user.
  • The app should call finishTransaction(_:) to complete the purchase.

This is what is recommended by Apple:

Your application should call finishTransaction(_:) only after it has successfully processed the transaction and unlocked the functionality purchased by the user.

  • A purchase is atomic when the app unlocks the functionality purchased by the user immediately and call finishTransaction(_:) at the same time. This is desirable if you're unlocking functionality that is already inside the app.

  • In cases when you need to make a request to your own server in order to unlock the functionality, you can use a non-atomic purchase instead.

  • Note: SwiftyStoreKit doesn't yet support downloading content hosted by Apple for non-consumable products. See this feature request.

SwiftyStoreKit provides three operations that can be performed atomically or non-atomically:

  • Making a purchase
  • Restoring purchases
  • Completing transactions on app launch