Search code examples
iosswiftin-app-purchase

What should happen when non-purchased user press the restore purchase button in iOS?


My sandbox test account can purchase non-consumable item and restore it. Everything works. However, if the account have not purchased the item before, pressing the restore button does nothing. I see nothing in the debug panel. I'm expecting iOS to detect if a certain user has purchased the item or not, if not then display a message asking them to buy it. Does it work like that or the current behavior is totally acceptable?

Here is the restore purchase code (Swift) connecting to a button inside the main storyboard:

@IBAction func restoreButtonPressed(sender: UIButton) {
    statusLabel.text = "Status: Restoring Purchase"
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

Other implemented methods include:

Works for normal purchase

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {}

Works for normal restore

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {}

Never see messages coming from this method before

func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!) {}

Thanks!


Solution

  • You can check if the queue has any returned transactions, and if not it means that there are no purchases to restore:

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
      if queue.transactions.count == 0 {
        let alert = UIAlertView()
        alert.title = "Oops"
        alert.message = "There are no purchases to restore, please buy one"
        alert.addButtonWithTitle("Buy")
        alert.addButtonWithTitle("Cancel")
        alert.show()
     }
    }