I use StoreKit's StoreView to buy in-app purchases. The purchase is working in the simulator, but how can I restore purchases? I did not find a restore handler, there is only a onInAppPurchaseCompletion
but I'm missing a onRestorePurchaseCompletion
or similar.
This is how it looks
This is my code:
StoreView(ids: ["my.product.id"])
.storeButton(.visible, for: .restorePurchases)
.storeButton(.hidden, for: .cancellation)
.onInAppPurchaseCompletion { product, result in
if case .success(.success(let transaction)) = result {
print("Purchased successfully: \(transaction.signedDate)")
isPremium = true
} else {
print("Something went wrong")
}
}
When you use StoreKit 2 with StoreView
, there's normally no need for existing customers to manually restore purchases. You can choose to make the "restore purchases" button visible for rare cases where a customer doesn't have access to an in-app purchase product they believe they should have access to.
I think the closest API to what you are requesting is currentEntitlementTask(for:priority:action:)
, which we also cover in the "Meet StoreKit for SwiftUI" session around the 16:40 timestamp.
The API I shared makes it easy to add a dependency on the current entitlement to any SwiftUI view that depends on someone owning your in-app purchase. You can also check this in imperative code using an API like currentEntitlement
on Product
. In either case, the API should have accurate information as soon as you call it without requiring your customers to manually use a restore button. If a customer activates the "restore purchases" button in your store view, and a missing transaction is restored (this is rare), all of the StoreKit 2 transaction APIs, including the two I mentioned, will be updated.