应用程序内购买ios发送超过需要的更多交易 [英] In-app purchase ios sending more transactions than needed

查看:83
本文介绍了应用程序内购买ios发送超过需要的更多交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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])

被叫2次,而不是3,4,5

called 2 times, than 3,4,5

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

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)

}

我还有:

 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
                        }

                    }


推荐答案

添加的不良做法

 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.

此外,您不再需要为您的userDefaults调用synchronize了从iOS 8开始,我仍然看到很多人在做这件事。

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

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

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

这篇关于应用程序内购买ios发送超过需要的更多交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆