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.