SKPaymentQueue updatedTransactions未被调用

时间:2016-05-21 11:20:50

标签: ios swift storekit skpaymenttransaction

我正在购买"此应用内购买已经被购买,它将免费恢复"但没有调用委托updatedTransactions,没有任何反应,IAP也没有执行。

我已经实现了正常运行的Restore Purchase,但是我如何阻止用户再次购买非耗材?为什么没有调用委托updatedTransactions(SKPaymentTransactionState.Restored)?

即使在删除并重新安装应用程序后进行购买,也会显示此消息。

这是我的代码。如果我做错了,请告诉我。谢谢

func makePaymentForProduct(product:SKProduct){
    let payment = SKPayment.init(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment)
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}

func restorePurchases(){
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}


//MARK: SKProductsRequestDelegate

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    self.delegate?.didGetProducts(response.products)
}

func request(request: SKRequest, didFailWithError error: NSError) {
    self.delegate?.purchaseFailed(error.localizedDescription)
}

//MARK: SKPaymentTransactionObserver

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    for (_, transaction) in transactions.enumerate() {
        switch (transaction.transactionState) {
        case SKPaymentTransactionState.Purchased:
            self.completeTransaction(transaction)
            break
        case SKPaymentTransactionState.Restored:
            self.restoreTransaction(transaction)
            break
        case SKPaymentTransactionState.Failed:
            self.failedTransaction(transaction)
            break
        default:
            break
        }
    }
}

func completeTransaction(transaction:SKPaymentTransaction){
    self.delegate?.purchaseSuccessful()
    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}

func restoreTransaction(transaction:SKPaymentTransaction){
    self.delegate?.purchaseSuccessful()
    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}

func failedTransaction(transaction:SKPaymentTransaction){
    self.delegate?.purchaseFailed("")
    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}


//Restore Purchase

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
    self.delegate?.purchaseRestored()
}

func paymentQueue(queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError) {
    self.delegate?.purchaseFailed(error.localizedDescription)
}

1 个答案:

答案 0 :(得分:2)

您在调用.restoreCompletedTransactions()之后设置了事务观察者,这可能是问题 - 我建议您尝试

func restorePurchases(){
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

而不是上面的实现。

作为旁注,我看到您的原始代码是在多个位置添加事务观察器 - 您可能会考虑尝试将其减少到一个位置(如果可能!)。