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

独自空忆成欢 提交于 2020-12-12 09:19:09

问题


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?


回答1:


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")
        }
    }
}


来源:https://stackoverflow.com/questions/64844940/swift-how-to-fix-strange-in-app-purchase-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!