[Ios] Swift,spritekit:在应用程序购买代码运行,什么都没有发生?



Answers

Question

好吧,我在Swift工作,我只需要帮助。 我已经遵循了4个不同的教程,关于如何使用Swift在Sprite工具包中的应用程序购买实现,逐字复制代码,并没有什么是为我工作。

以下是我采取的步骤:

  1. 走进iTunes连接,并创建一个应用程序购买下我的应用程序的记录。 我在应用程序购买的产品ID是"GameOverSaveSavior"

  2. 在Xcode中,我将应用程序的应用程序购买功能设置为ON ,确保将我的团队设置为我的帐户,并确保将信息下的Bundle标识符设置为ITunes Connect中我的应用程序的包标识符

  3. 在编写任何代码之前,我在我的GameScene.swift文件中import StoreKit

至于代码,这是我所做的:

(1)在Gamescene.swift中,在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")
        }

当应用程序运行时输出"IAP is enabled, loading"

(2)在GameScene.swift中,在类内但在didMoveToView之外,我有所有的功能和变量,其他人用于应用程序购买:

var list = [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)

            list.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");
    }

这会在应用程序运行时向控制台输出"product request"

(3)在GameScene.swift中,在我的touchesBegan函数中,当触摸右键时,我有以下几点:

//In app purchase
            if touchedNode == saveMeBtn {

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

这输出"button touched!" 当按钮被触摸时。

我不知道我做错了什么。 代码编译没有错误,但是当我的按钮被触摸时没有任何事情发生。 没有提醒要求用户登录或购买任何东西 - 什么都没有。 我主要关注这个问题: 在SKScene的应用程序购买

在编写代码之前还有什么应该做的吗? 我在代码中丢失了什么吗?




Links