Search code examples
swiftsprite-kitin-app-purchaseskscene

In app purchase in SKScene not working?


I'm creating a SpriteKit game in Swift and I'm trying to implement in app purchases.

I followed this question here: in app purchase in SKScene

And here is my code:

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

Outside of didMoveToView but in 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 touchesBegan for when the in app purchase node is touched:

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

All of my code is straight from the question above, and the code compiles without any errors and

IAP is enabled, loading

and

product request

is printed to the console at the start of the game.

When the in app purchase button is touched,

button touched!

is printed to the console but nothing else happens. It doesn't ask the user to purchase anything.

I made sure that the bundle ID in Xcode is the same as in iTunes Connect and that the in app purchase ID is the same. What am I doing wrong here?


Solution

  • After a good few weeks of work, I found out that you must have your Paid Applications agreement filled out completely and accepted in order for your code and products to work. Approval can take a few days, then you're in the clear.