应用程序内购买ios发送的交易超过了所需的数量

时间:2015-12-08 22:28:56

标签: ios in-app-purchase

我在SpriteKit中使用应用内购买。第一笔交易没问题,但是当我做第二笔交易时我的

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

调用2次,而不是3,4,5

所以我做了一个请求,但它增加了100个硬币而不是50个......

我想到了这个功能中的问题:

 func buyProduct() {
    print("buy +" + p.productIdentifier)

    var payment = SKMutablePayment(product: p)


    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().addPayment(payment)

}

我也有:

 func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    var myProduct = response.products
            for product in myProduct {
                list.append(product)
                print("product added")
                print(product.productIdentifier)
                print(product.localizedTitle)


            }
    print("list is \(list)")

}

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    print(transactions.count)
    for transaction in transactions {
        print(transaction.error)

        switch transaction.transactionState {
        case .Purchased:
            MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
            print("buyed")
            print(p.productIdentifier)
            let prodID = p.productIdentifier

            switch prodID {
            case "com.addCoins" :
                print("increaing coinsCount")
                coinsCount = coinsCount + 50
                let userDefaults = NSUserDefaults.standardUserDefaults()
                userDefaults.setInteger(coinsCount, forKey: "coins")
                userDefaults.synchronize() // don't forget this!!!!
                coinsLabel.text = String(coinsCount)
            default: print("IAD not setuped")
            }

            print("premium added")
            queue.finishTransaction(transaction)
            break
        case .Failed:
            MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
            print("buy error")
            queue.finishTransaction(transaction)
            break
        default: break
        }
    }
}
我打电话给购买:

for product in list {
                        let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
                        loadingNotification.mode = MBProgressHUDMode.Indeterminate
                        loadingNotification.labelText = "Loading"

                        let prodID = product.productIdentifier
                        if prodID == "com.addCoins" {
                            p = product
                            buyProduct()
                            break
                        }

                    }

2 个答案:

答案 0 :(得分:0)

在某些时候,您必须告诉Apple已经进行了购买并且您已交付货物。在此之前,购买将永远留在交易队列中。

答案 1 :(得分:0)

添加

的不良做法
 SKPaymentQueue.defaultQueue().addTransactionObserver(self)

每次你买东西。您应该在应用启动时添加交易观察员,并在应用关闭时将其删除,这符合苹果推荐指南。

此外,自iOS 8以来,您不再需要为userDefaults调用synchronize,但我仍然看到很多人都在这样做。

关于你的问题代码似乎没问题。到目前为止我唯一注意到的是为什么在买入功能中你添加了SKMutablePayment? 尝试将SKMutablePayment更改为SKPayment,看看它是否有所作为。

  func buyProduct() {
print("buy +" + p.productIdentifier)

var payment = SKPayment(product: p)

SKPaymentQueue.defaultQueue().addTransactionObserver(self) // should not be here
SKPaymentQueue.defaultQueue().addPayment(payment)
}