UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

What right way to restore in app purchases with swift and handle non purchased product

Forums > Swift

@Hell  

How to handle that the user did not make any purchases when restoring purchases?

I have function that handle purchased products

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]){
        for transaction in transactions {
            switch transaction.transactionState
            {
            ...

                transactionState = .purchased
            case .restored:

                //HANDLE purchased here

                queue.finishTransaction(transaction)

                transactionState = .restored
            ...
            }
        }
    }

Next

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue){
  for transaction in queue.transactions
  {
      //do something
  }
}

And

func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
     //HANDLE error here
}

And call restoring with

func restoreProducts(){
   SKPaymentQueue.default().restoreCompletedTransactions()
}

If the user made a purchase before, I can perfectly track this in the first function

If there were no purchases, none of the three processing functions are called.

Q: How to track in this case?

Thanks

3      

If there were no purchases, none of the three processing functions are called.

I have the same question. Did you ever figure it out?

3      

I have no experience with StoreKit but have you seen this video from WWDC last year?

https://developer.apple.com/videos/play/wwdc2022/110404/

3      

In your code, you need to be able to save in backend the user that makes a purchase then you can try it like below ,when a user tries to restore to check if its id exists in backend


 @IBAction func restorePurchaseTapped(_ sender: UIButton) {

        if let user = purchasedDoneUser?.id {

            SKPaymentQueue.default().add(self)
            let request = SKReceiptRefreshRequest(receiptProperties: nil)
            request.delegate = self
            request.start()

            SKPaymentQueue.default().restoreCompletedTransactions()

        } else {

            // --

        }

    }

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.