Search code examples
iosswiftin-app-purchaseswiftystorekit

Restoring auto-renewable subscriptions after


We are using auto-renewable subscriptions, and it works ok except for the one edge case.

If user reset their iPhone to the factory settings, it's not possible for them to restore auto-renewable subscription in our app (or so it seems, we have just two such cases now).

We are using SwiftyStoreKit, but I believe that there is a logic error on our side.

Right now we are restoring purchases this way:

func checkOldSubscriptions()->Bool {

     //validator
     let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: ss)

      for i in 0..<products.count {

           //checking every available product in receipt
           SwiftyStoreKit.verifyReceipt(using: appleValidator, completion: { (result) in
               switch result {
                   case .success(let receipt):

                let purchaseResult = SwiftyStoreKit.verifySubscription(
                    type: .autoRenewable, 
                    productId: products[i].name,
                    inReceipt: receipt)

                    switch purchaseResult {
                        case .purchased(let expiresDate):
                        print("Product is valid until \(expiresDate)")

       ...
       }
       }

My hypothesis is that after the reset we just don't have any receipt to verify, and I have to retrieve it directly. It's kind of hard to check on device because, honestly, I don't have a real test device that would be easy to kill for me.

So I want to add something like that (manually refreshing the receipt before trying to verifying it):

SwiftyStoreKit.verifyReceipt(using: appleValidator, forceRefresh: true) { result in
    switch result {
    case .success(let receipt):

Does it make any sense? Will it help?


Solution

  • You are correct. A receipt is not likely to be available after a reset. You need to call restoreCompletedTransactions() on SKPaymentQueue to retrieve any transactions that were previously marked completed.

    Since you are using SwiftyStoreKit you can use SwiftyStoreKit.restorePurchases. Check the documentation for details.