Listen to when the in-app purchase window opens on iOS

92 views Asked by At
SKPaymentQueue.default().add(payment)

I'm starting an in-app purchase with. But I think the purchase window sometimes opens late. Is there a method, delegate method to listen for the situation where this screen opens?

I researched this but I could not reach a conclusion, does anyone know?

2

There are 2 answers

0
Tomo Norbert On

I suggest using a very well made (and easy to user) library for handling in-app purchases. It is called SwiftyStoreKit. enter link description here We use it in many projects and it has nice closures while handling purchasing. You can put your UI blocking progress indicator just before calling it's methods and remove when the closure returns with a result.

0
Omer Faruk Ozturk On

You can use the delegate below to handle it (ex. show/hide progress view) regarding to SKPaymentTransactionState:

// Handle transaction status after you call .add(payment).
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction:AnyObject in transactions {
        if let trans = transaction as? SKPaymentTransaction {
            switch trans.transactionState {
            case .purchased:                 
                SKPaymentQueue.default().finishTransaction(trans)
                // purchased..
            case .failed:
                SKPaymentQueue.default().finishTransaction(trans)
                // failed ..
            case .restored:
                SKPaymentQueue.default().finishTransaction(trans)
                // restored
            default:
                break
            }
        }
    }
}