How to Restore Purchases in SwiftUI

How to Restore Purchases in SwiftUI
Quick answer: Restore purchases means recovering access to eligible items already bought with the customer’s App Store account, without charging again. In a StoreKit 2 app, provide a user-initiated Restore Purchases button that calls try await AppStore.sync(), then rebuild access from verified Transaction.currentEntitlements. Older StoreKit implementations use SKPaymentQueue.default().restoreCompletedTransactions() and handle restored transactions through a payment queue observer.

Why this happens

An in-app purchase belongs to the customer’s App Store purchase history, not just to the installation that originally made the purchase. Deleting an app can remove its local record of unlocked features. Installing it on another device also starts without that installation’s local state.

Restoring purchases reconnects the app’s access state with eligible purchases already associated with the customer’s App Store account. It is not another purchase and does not charge the customer again. Your app must still read the resulting transactions, verify them, and unlock the appropriate content.

The implementation depends on your StoreKit version. StoreKit 2 provides current entitlements and an explicit synchronization API. Original StoreKit delivers restored transactions through a payment queue observer. Neither approach makes every product type restorable.

How to fix it

1. Identify which products can be restored

Before adding a button, decide what the app should recover:

Product type What restoration means
Non-consumable Recover a permanent purchase, such as a premium feature unlock, if the purchase remains valid.
Auto-renewable subscription Recover access when the customer currently has an applicable subscription entitlement.
Consumable StoreKit restoration does not replenish previously delivered coins, credits, or other consumables.
Non-renewing subscription Your app must track the access period and provide its own appropriate recovery logic; do not treat it like an auto-renewable subscription.

For apps with restorable purchases, provide an accessible restore mechanism, commonly a Restore Purchases button on the purchase screen or in settings.

Do not interpret a successful restore operation as proof that a particular feature should be unlocked. A customer may have no matching purchases, an expired subscription, or a refunded purchase. Access must come from the verified entitlement state.

2. Use StoreKit 2 for a SwiftUI implementation

For apps using StoreKit 2, available starting with iOS 15, call AppStore.sync() in response to an explicit user action. After synchronization completes, read Transaction.currentEntitlements and rebuild the app’s access state.

This focused example supports one non-consumable premium unlock and one auto-renewable premium subscription:

import SwiftUI
import StoreKit

struct RestorePurchasesView: View {
    @State private var hasPremium = false
    @State private var isRestoring = false
    @State private var message: String?

    private let premiumProductIDs: Set<String> = [
        "com.example.app.premium",
        "com.example.app.premium.monthly"
    ]

    var body: some View {
        VStack(spacing: 16) {
            Text(hasPremium ? "Premium is available" : "Premium is locked")

            Button(isRestoring ? "Restoring…" : "Restore Purchases") {
                Task { @MainActor in
                    await restorePurchases()
                }
            }
            .disabled(isRestoring)

            if let message = message {
                Text(message)
                    .font(.footnote)
            }
        }
        .padding()
        .task {
            await refreshEntitlements()
        }
    }

    @MainActor
    private func restorePurchases() async {
        isRestoring = true
        message = nil
        defer { isRestoring = false }

        do {
            try await AppStore.sync()
            await refreshEntitlements()

            message = hasPremium
                ? "Your premium access is available."
                : "No eligible premium purchases were found."
        } catch {
            message = "Purchases could not be restored: \(error.localizedDescription)"
        }
    }

    @MainActor
    private func refreshEntitlements() async {
        var premiumIsAvailable = false

        for await result in Transaction.currentEntitlements {
            guard case .verified(let transaction) = result else {
                continue
            }

            guard premiumProductIDs.contains(transaction.productID) else {
                continue
            }

            switch transaction.productType {
            case .nonConsumable, .autoRenewable:
                premiumIsAvailable = true
            default:
                break
            }
        }

        hasPremium = premiumIsAvailable
    }
}

Replace the example product identifiers with the identifiers configured for your app. The allowlist matters: owning an unrelated product should not unlock premium access.

The example also deliberately accepts only verified results. An unverified transaction is not evidence that your app should grant access.

Notice that the refresh starts with false and recomputes the result. It does not permanently preserve an old local unlock just because the app previously saw a purchase. In a production app, put this logic in a shared purchase manager so every screen uses the same entitlement state.

3. Refresh access normally without forcing synchronization

The explicit restore button is a recovery path, not the only way customers should regain access.

StoreKit 2 normally makes transaction and entitlement information available without requiring an AppStore.sync() call every time the app launches. Read Transaction.currentEntitlements when initializing your purchase state, as the example does when the view appears.

Also maintain a long-lived listener for Transaction.updates in your purchase manager. Verify incoming transactions, update access, and finish transactions after delivering the associated content or service. This handles changes that arrive while the app is running rather than relying on the customer to reopen the restore screen.

Reserve AppStore.sync() for a user-requested recovery action. It can require App Store authentication, so calling it automatically on every launch can create an unnecessary interruption.

4. Handle restored transactions in older StoreKit apps

If your app still uses the original StoreKit API, the restore entry point is:

import StoreKit

func restorePurchases() {
    SKPaymentQueue.default().restoreCompletedTransactions()
}

That call starts restoration; it does not directly return the purchased products. Register and retain an SKPaymentTransactionObserver, then handle the callbacks:

  • In paymentQueue(_:updatedTransactions:), process transactions whose state is .restored.
  • Use the restored transaction and its original transaction information as appropriate to identify the purchase.
  • Validate the purchase before unlocking content, then call finishTransaction(_:) after processing it.
  • Use paymentQueueRestoreCompletedTransactionsFinished(_:) to stop the progress indicator after restoration completes.
  • Handle paymentQueue(_:restoreCompletedTransactionsFailedWithError:) to report failures and re-enable the button.

A completion callback alone does not prove that any matching purchase was restored. Track the restored access separately so your interface can distinguish “finished” from “found an eligible purchase.”

Keep the observer active throughout restoration. Adding a button without implementing the observer’s restored-transaction handling leaves the recovery flow incomplete.

5. Check the account and product when nothing returns

If restoration completes but access remains locked, check these points before changing the restore API:

  1. Confirm the customer is using the App Store account that made the purchase. Your app’s own login is not necessarily the same account.
  2. Confirm the purchased product identifier matches the identifier your entitlement code recognizes.
  3. Check whether the item is a consumable or an expired subscription rather than a currently restorable entitlement.
  4. Confirm verification succeeded and your interface is reading the refreshed purchase state.
  5. During development, distinguish local StoreKit testing, sandbox purchases, and production purchases rather than expecting their histories to be interchangeable.

For consumable balances maintained by your service, recover the balance through your account system. Do not grant the original quantity again merely because the customer taps Restore Purchases.

How to prevent it

  • Treat verified StoreKit entitlements as the source of purchase access, not a single local Boolean.
  • Include a visible restore mechanism wherever customers manage restorable purchases.
  • Refresh entitlements during normal app initialization and observe transaction updates.
  • Keep product-to-feature mappings in one shared purchase manager.
  • Test reinstall, second-device access, no matching purchases, expiration, and restore failure.
  • Explain which account owns the purchase without asking customers to buy the same unlock again.

FAQ

Does Restore Purchases charge the customer again?

No. Restoration recovers access to eligible existing purchases; it does not initiate a new purchase. The app must still determine which verified purchases currently grant access.

Do I need a restore button if StoreKit 2 loads purchases automatically?

Provide an explicit restore mechanism for restorable purchases even when normal entitlement loading usually recovers access automatically. In StoreKit 2, that user action can call AppStore.sync() and then refresh current entitlements.

Why does Restore Purchases not bring back coins or an expired subscription?

Previously delivered consumables are not restored through StoreKit’s restore flow. An expired subscription does not provide current access merely because it appears in purchase history; non-renewing subscriptions require your own duration and recovery logic.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.