Search code examples
iosswiftuser-interfacein-app-purchasein-app-subscription

Swift: how to fix strange in-app purchase behavior?


I use code for in-app purchase based on this answer. But I ran into some strange bugs. For example:

I have FirstViewController with locked content and PurchasesViewController with purchase buttons. When I click on purchase button in PurchasesViewController, confirm purchase and wait several seconds for notification about the purchase is successful. Next I go back to FirstViewController and see that content unlocked. In this example all works fine. But...

Problem:

If I click on purchase button in PurchasesViewController, confirm purchase and without waiting for the notification, I go back to FirstViewController and receive a notification there. My content not unlocked. Even if I restart the app content not unlocked. But if I click on restore purchases button all start works fine and locked content will be unlocked.

This problem can be confusing to the user. So I want to lock the interface and show the activity indicator until the user receive a notification about the purchase is successful. And after user click "Ok" in notification windows I want to unlock interface and remove activity indicator. But how to do it? Where and when I should call lock interface function?


Solution

  • On my projects I usually use https://github.com/SVProgressHUD/SVProgressHUD for show loadings. And on your case you need to show SVProgress.show() when user tap on purchase button. And add SVProgress.dismiss() when all purchased finished SVProgress.showErrorWithStatus(error.localizedDescription) when you have payment issue.

    On SKPaymentTransactionObserver have func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) on this function you can check status paymentQueue like this example:

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for t in transactions {
            switch t.transactionState {
            case .purchasing, .deferred: break // do nothing
            case .purchased, .restored:
                let p = t.payment
                if p.productIdentifier == whatever {
                    queue.finishTransaction(t)
                    SVProgress.dismiss() 
                }
            case .failed:
                queue.finishTransaction(t)
                SVProgress.showErrorWithStatus("Error message")
            }
        }
    }