How do I listen to restore purchase response in Storekit 2?

74 views Asked by At

I'm trying to restore an individual purchase under iOS 17 and StoreKit 2. After tapping the restore button I need to download a file to complete the necessary restore process.

How do I listen to a restored purchase to download the necessary file?

I'm starting the restore process with this code:

.toolbar {
    ToolbarItem {
        Button("Restore") {
            isRestoring = true
            Task.detached {
                defer { isRestoring = false }
                try await AppStore.sync()
            }
        }
        .disabled(isRestoring)
    }
}

When I tap the restore button the only thing it does is show an Alert with the message: "Sign in with Apple ID. Press OK to simulate authenthicating with an Apple ID [Enviroment: Xcode]" And nothing else happens.

I need to know when the process is complete to download a file.

Thank you!

1

There are 1 answers

0
neowinston On

I found this sample code on Apple's documentation (currentEntitlements) that worked for me.

func refreshPurchasedProducts() async {
    // Iterate through the user's purchased products.
    for await verificationResult in Transaction.currentEntitlements {
        switch verificationResult {
        case .verified(let transaction):
            // Check the type of product for the transaction
            // and provide access to the content as appropriate.
            ...
        case .unverified(let unverifiedTransaction, let verificationError):
            // Handle unverified transactions based on your
            // business model.
            ...
        }
    }
}