Best practice to add a transaction queue observer at application launch for Storekit

荒凉一梦 提交于 2019-12-02 10:16:17

In the end I used SwiftyStoreKit which solved the problem for me. I highly recommend it.

Edit to show SwiftyStoreKit.completeTransactions which goes in didFinishLaunchingWithOptions() in appDelegate. unlockIAPContent() is where I put the purchase logic and this will handle promo codes and unfinished transactions:

    // This registers the transaction observer and listens for unfinished transactions
    SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
        for purchase in purchases {
            switch purchase.transaction.transactionState {
            case .purchased, .restored: 

                // Unlock content
                self.unlockIAPContent(productID: purchase.productId) 

                if purchase.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(purchase.transaction)
                }

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