Tunds
Tunds

Reputation: 1817

Restoring purchases in swift

So i can't seem to get the updatedTransactions protocol to fire when trying to restore purchases.

I have a button in one view controller which calls the following method in my IAPViewController file restoreIAP() which is set up like so.

func restoreIAP(){

    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

This method is called when the user presses the button so this is the class which handles this.

class SettingsViewController: IAPViewController {


    @IBAction func restoreDidTouch(sender: AnyObject) {

        restoreIAP()
        activityTitle = "Restoring"

    }

}

In my IAPViewController nothing seems to be triggering this method so that i can do something.

// Check the transaction
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    // Check the tranactions

    for transaction in transactions {

        switch transaction.transactionState {

        case .Purchasing:
            // TODO: Start Activity Indicator
            showPurchaseIndicator(activityTitle)
            break

        case .Purchased:
            // TODO: End the purchasing activity indicator
            dismissPurchaseIndicator()
            print("Transaction completed successfully.")
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            transactionInProgress = false

            // TODO: Put method here to unlock all news sources
            storiesMethods.unlockAllStories()
            break

        case .Restored:
            // TODO: Start Activity Indicator
          //  showPurchaseIndicator(activityTitle)
            break

        case .Failed:
            dismissPurchaseIndicator()
            notificationMethods.showAlertErrorMessage(self, title: "Purchase", actionMessage: "Dismiss", message: "Unable to complete transaction please try again later.")
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            transactionInProgress = false
            break

        default:
            print(transaction.transactionState.rawValue)
            break
        }
    }

}

Upvotes: 5

Views: 1790

Answers (2)

Federico Zanetello
Federico Zanetello

Reputation: 3451

From your description and the code snippets it looks like everything is in the right order.

If the paymentQueue function is never called, your IAPViewController probably doesn't conform to the SKPaymentTransactionObserver protocol, just make it conform:

class IAPViewController: UIViewController, SKPaymentTransactionObserver

and you're good to go.

Upvotes: 0

phimage
phimage

Reputation: 274

Did your controller added as observer using SKPaymentQueue.defaultQueue().addTransactionObserver(..)?

PS: Have a look at SwiftyStoreKit ( InAppProductPurchaseRequest.swift )

Upvotes: 2

Related Questions