swift - SKScene 中的应用程序购买不起作用?

标签 swift sprite-kit in-app-purchase skscene

我正在 Swift 中创建 SpriteKit 游戏,并尝试在应用内购买中实现。

我在这里关注了这个问题:in app purchase in SKScene

这是我的代码:

在 didMoveToView 中:

 // Set IAPS
        if(SKPaymentQueue.canMakePayments()) {
            println("IAP is enabled, loading")
            var productID:NSSet = NSSet(objects: "GameOverSaveSavior")
            var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
            request.delegate = self
            request.start()
        } else {
            println("please enable IAPS")
        }

在 didMoveToView 之外但在 GameScene 中:

 //IN APP PURCHASE ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    var productList = [SKProduct]()
    var p = SKProduct()

    func purchaseMade() {
        println("they bought it!")
    }

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

        var pay = SKPayment(product: p)
        SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
    }
    func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
        println("product request")
        var myProduct = response.products

        for product in myProduct {
            println("product added")
            println(product.productIdentifier)
            println(product.localizedTitle)
            println(product.localizedDescription)
            println(product.price)

            productList.append(product as! SKProduct)
        }
    }

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
        println("transactions restored")

        var purchasedItemIDS = []
        for transaction in queue.transactions {
            var t: SKPaymentTransaction = transaction as! SKPaymentTransaction

            let prodID = t.payment.productIdentifier as String

            switch prodID {
            case "GameOverSaveSavior":

                purchaseMade()

                //Right here is where you should put the function that you want to execute when your in app purchase is complete
            default:
                println("IAP not setup")
            }

        }

        var alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored. You may have to restart the app before banner ads are removed.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
    }


    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
        println("add paymnet")

        for transaction:AnyObject in transactions {
            var trans = transaction as! SKPaymentTransaction
            println(trans.error)

            switch trans.transactionState {

            case .Purchased, .Restored:
                println("buy, ok unlock iap here")
                println(p.productIdentifier)

                let prodID = p.productIdentifier as String
                switch prodID {
                case "GameOverSaveSavior":

                    //Here you should put the function you want to execute when the purchase is complete
                    var alert = UIAlertView(title: "Thank You", message: "You may have to restart the app before the banner ads are removed.", delegate: nil, cancelButtonTitle: "OK")
                    alert.show()
                default:
                    println("IAP not setup")
                }

                queue.finishTransaction(trans)
                break;
            case .Failed:
                println("buy error")
                queue.finishTransaction(trans)
                break;
            default:
                println("default")
                break;

            }
        }
    }

    func finishTransaction(trans:SKPaymentTransaction)
    {
        println("finish trans")
    }
    func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
    {
        println("remove trans");
    }

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

在触摸应用内购买节点时开始:

//In app purchase
            if touchedNode == saveMeBtn {

                println("button touched!")
                for product in productList {
                    var prodID = product.productIdentifier
                    if(prodID == "GameOverSaveSavior") {
                        p = product
                        buyProduct()  //This is one of the functions we added earlier
                        break;
                    }
                }

我的所有代码都是直接来自上面的问题,并且代码编译时没有任何错误,

IAP is enabled, loading

product request

在游戏开始时打印到控制台。

当触摸应用内购买按钮时,

button touched!

被打印到控制台,但没有任何其他 react 。它不要求用户购买任何东西。

我确保 Xcode 中的 bundle ID 与 iTunes Connect 中的相同,并且应用内购买 ID 相同。我在这里做错了什么?

最佳答案

经过几周的工作,我发现您必须完整填写并接受付费应用程序协议(protocol),才能使您的代码和产品正常运行。批准可能需要几天时间,然后您就可以了。

关于swift - SKScene 中的应用程序购买不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32303801/

相关文章:

ios - 让 ScrollView 与自动布局和 Storyboard一起工作

ios - Swift iOS 应用程序 - 使用动态更改的行静态分配表属性

ios - didMove之间有什么区别(查看: SKView) and didMoveToView(view: SKView)?

ios - else if 条件不执行语句

ios - 当以编程方式单击 UITabBarItem 时,如何将 ViewController 推送到 NavigationController?

ios - SDWebImage 使用 Swift 将图像设置为 CellView 中的 UIButton

ios - SK3DNode不旋转

Android In App Billing 恢复交易

ios - 应用内购买成功导致部分用户崩溃

in-app-purchase - 是否有必要从 "Google Play Billing with AIDL"迁移到 "Google Play Billing Library"?