How to restore non renewing purchases in swift 3?

I am having trouble restoring non-renewing purchases. This is how it is set up:

There is a page on the app where there are all the products that a user can buy. Also, there is a restore button on top of the page. Now if the user buys any products and wants to restore the purchase on another device he has, he should be able to click the restore button and all his previous purchases should be restored. I have written the code that should restore the purchases. Once, the user clicks I call SKPaymentQueue.default().restoreCompletedTransactions(). After this is called it should call func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) , which it does but the queue is always empty. I have copied and pasted a snippet of the code to this email. Can you please help me resolve this?


@IBAction func restoreButton(_ sender: AnyObject) {
        if(daysBetweenDates(startDate: (user?.purchases.date)!, endDate: NSDate() as Date) < 90){
            if(SKPaymentQueue.canMakePayments()){
                SKPaymentQueue.default().add(self)
                SKPaymentQueue.default().restoreCompletedTransactions()
              
            } else {
                showiTunesError()
            }
        } else {
            showRestoreMessage()
        }
    }



func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
        for transaction in queue.transactions{
            let trans: SKPaymentTransaction = transaction as SKPaymentTransaction
          
                let prodID = (trans.original?.payment.productIdentifier)! as String
                print(prodID)
                switch prodID {
                case "productID":
                    user?.purchases.chapters = true
                default:
                    print("some")
                }
        }
        updatePurchases()
    }


func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
      
        for trans:AnyObject in queue.transactions{
            let trans: SKPaymentTransaction = trans as! SKPaymentTransaction
            let prodID = trans.payment.productIdentifier as String
            print(trans.transactionState.rawValue)
            print(prodID)
            switch (trans.transactionState) {
            case .purchased:
                switch prodID {
                case "productID":
                    user?.purchases.chapters = true
                    updatePurchases()
                default:
                    print("defualt")
                }
                SKPaymentQueue.default().finishTransaction(trans)
                SKPaymentQueue.default().remove(self)
                break;
              
            case .failed:
                SKPaymentQueue.default().finishTransaction(trans)
                SKPaymentQueue.default().remove(self)
                break;
              
            default:
                break;
            }
        }
    }



Best regards,

Chirag.

Replies

SKPaymentQueue's restoreCompletedTransactions allows you to restore previously bough non-consumable products and auto-renewable subscriptions. It does not restore non-renewable subscriptions. You must implement your own mechanism for restoring non-renewable subscriptions as mentioned in https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/Products.html#//apple_ref/doc/uid/TP40008267-CH2-SW4


Non-renewable subscriptions. Subscriptions that don’t involve delivering episodic content. Examples include access to a database of historic photos or a collection of flight maps. It’s your app’s responsibility to make the subscription available on all of the user’s devices and to let users restore the purchase. This product type is often used when your users already have an account on your server that you ca

As noted above, you have to implement your own method of transfering a non-renewable subscription to other devices owned by the same user. One straightforward way of doing that is recording the purchase of a non-renewing subscription to the key-value file in the user's iCloud Account. Your 'restore' button would then check that key-vlue file to see if some other device recorded a subscription.

Invalid code