Reputation: 347
I'm going a bit crazy trying to implement subscriptions into my app through StoreKit 2.
Everything generally works fine the first time, but after a subscription has expired, when I try and re-subscribe, I get inconsistent behavior. Sometimes it will work okay, though very slowly. Most of the time however, product.purchase() provides a .success, though does not actually resubscribe, and does not prompt the user with any "confirm subscription" screen. Then, when checking the Transactions, the code correctly returns nil, seeing as we haven't actually renewed the subscription.
Is this just Sandbox behavior? I'm worried about putting the code into production with this bizarre issue.
My full code is similar to this - https://www.revenuecat.com/blog/engineering/ios-in-app-subscription-tutorial-with-storekit-2-and-swift/
UPDATE: Perhaps it is just the Apple Sandbox servers being garbage - if I mash the button over and over, eventually the "confirm subscription" screen does come up?
Upvotes: 5
Views: 510
Reputation: 2089
I was struggling around this issue for a while, then in my case I found that it is related to unfinished transactions.
Be sure to finish the transaction when your application receives a subscription renewal update.
func observeTransactionUpdates() -> Task<Void, Never> {
Task(priority: .background) { [unowned self] in
for await result in Transaction.updates {
//my own function for retrieving the purchased product
await self.fetchPurchasedProducts()
//Always finish a transaction.
if case .verified(let transaction) = result {
await transaction.finish()
}
}
}
}
Another useful trick is to review all unfinished transactions related to the subscription and finish them before attempting to buy a new one.
you can do something similar:
for await result in Transaction.unfinished {
if case .verified(let transaction) = result {
await transaction.finish()
}
}
A specific thread about it on Apple dev forum
https://forums.developer.apple.com/forums/thread/723126
Upvotes: 1