coding22
coding22

Reputation: 689

Why doesn't my In App Purchase purchase the product?

I'm calling this function when I press a button to unlock a level but nothing happens when I do this. It runs the function but it doesn't ask for my password or anything else. What am I doing wrong?

func callthis() {

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

    }
}



func callthis2() {

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

    }
}



func callthis3() {

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

    }
}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {



    var touch: UITouch = touches.first as! UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)


    if node.name == "unlockleveltwobutton" {
        callThis()

        for product in list {
            var prodID = product.productIdentifier
            if(prodID == "unlockLevelTwoPlease") {
                p = product
                buyProduct()
                break;


            }
        }
        runAction(menuAudioPlayer)



    }


    if node.name == "unlocklevelthreebutton" {

        callThis2()

        for product in list {
            var prodID = product.productIdentifier
            if(prodID == "unlockLevelThreePlease") {
                p = product
                buyProduct()
                break;
            }

        }
        runAction(menuAudioPlayer)





    }


    if node.name == "unlocklevelfourbutton" {


        callthis3()


        for product in list {
            var prodID = product.productIdentifier
            if(prodID == "unlockLevelFourPlease") {
                p = product
                buyProduct()
                break;

            }
        }
        runAction(menuAudioPlayer)


    }

Upvotes: 0

Views: 131

Answers (1)

Nirav Bhatt
Nirav Bhatt

Reputation: 6969

From your code it looks like you haven't identified the asynchronous mode of in-app purchases. The whole process is done in steps, with various actors performing each step, and it cannot be described by the linear calls you have listed above.

In-app purchase is broadly made up of 3 distinct steps:

1 - Getting products from itunes store and displaying them on UI (Store UI which displays product titles, description, price in user-understandable way)

2 - Providing mechanism for user to purchase products from UI (IBActions that enable "Buy" action)

3 - Handling post-purchase (handle successful purchase by unlocking the product, handle failed purchase by displaying user-understandable error, and handle future restore for non-consumable products as they must not be purchased again as per the rules set by Apple)

This is what you must do. If you are looking for step by step tutorial on how to integrate IAP - here is my own tutorial to simplify what I wrote above.

If you need more efficient solution, here is video lecture series with SWIFT and Objective C. It also accompanies code samples for both.

Upvotes: 1

Related Questions