Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How restore with StoreKit 2?

Tags:

ios

storekit

iOS 15 introduces StoreKit 2. I'm looking at it to see whether I can adopt it in my existing apps, and I don't see how to do it. In particular, I don't see how to implement the required Restore functionality (in case the user has deleted my app, for instance).

I presume we are supposed to use Transaction.latest(for:)? Is that right?

But in my testing, it appears that if a user has done the purchase with StoreKit 1, that call returns nil. Is this true? Or if I'm doing it wrong, what's the correct way to migrate from StoreKit 1 to StoreKit 2, and how do we handle Restore?

like image 204
matt Avatar asked Dec 06 '25 17:12

matt


1 Answers

In Apple's SKDemo, they do this

Button("Restore Purchases", action: {
            async {
                //This call displays a system prompt that asks users to authenticate with their App Store credentials.
                //Call this function only in response to an explicit user action, such as tapping a button.
                try? await AppStore.sync()
            }
        })

I personally wanted to know if it was successful or not, so I wrapped it in a method

func restore() async -> Bool {
    return ((try? await AppStore.sync()) != nil)
}

And popped an alert accordingly

like image 182
Manas Avatar answered Dec 08 '25 05:12

Manas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!