In-app purchase ios sending more transactions than needed

孤街醉人 提交于 2019-12-12 04:14:59

问题


Im using in-app purchase in SpriteKit. The first transaction does fine, but when i do the second one my

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

called 2 times, than 3,4,5

so i do one request but there it adds 100 coins instead of 50...

i gues the problem in that function:

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

    var payment = SKMutablePayment(product: p)


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

}

i also have :

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

im calling purchase with:

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
                        }

                    }

回答1:


At some point you have to tell Apple that a purchase has been carried out and you delivered the goods. Until then, the purchases stay in the transaction queue forever.




回答2:


Its bad practice to add

 SKPaymentQueue.defaultQueue().addTransactionObserver(self)

everytime you buy something. You should add the transaction observer at app launch and remove it when your app is closed, as per apples recommended guidelines.

Also you dont need to call synchronise for your userDefaults anymore since iOS 8, yet I still see a lot of people doing it.

In regards to you problem the code seems to be ok. The only thing I noticed so far is why in the buy function are you adding a SKMutablePayment? Try changing SKMutablePayment to SKPayment and see if it makes a difference.

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

var payment = SKPayment(product: p)

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


来源:https://stackoverflow.com/questions/34167286/in-app-purchase-ios-sending-more-transactions-than-needed

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