0
votes

I have a button in an SKScene (using Swift) -- "Add Puzzles" When the user clicks this button, I want to go to a UIView Controller or UI Scene so that I can implement In App Purchase code... (SkScenes are subsets of UIViews and cannot run the code...)

Does anyone have a basic idea how to do this...

I am a little inexperienced at this... I have used cocos2d and now Sprite Kit and have always set everything up manually and have very little experience with .xib files and view Controllers...

Does anyone know the basic idea of how to do this? Thanks for any help!

ANDY

1
This answer demonstrates how an SKScene can communicate with a UIViewController: stackoverflow.com/a/22555881/2043580ZeMoon

1 Answers

3
votes

You can set-up In-App-Purchases inside of an SKScene, You may need to tweak some code but it will work. Here's an example

import Spritekit
import Storekit


class Store:SKScene, SKProductsRequestDelegate, SKPaymentTransactionObserver { 

override func didMoveToView {

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

}

// 2
func btnRemoveAds() {
    for product in list {
        var prodID = product.productIdentifier
        if(prodID == "bundle id") {
            p = product
            buyProduct()
            break;
        }
    }

}

// 3
func btnAddCoins() {
    for product in list {
        var prodID = product.productIdentifier
        if(prodID == "bundle id") {
            p = product
            buyProduct()
            break;
        }
    }
}


// 4
func removeAds() {
    println("ads removed")
}

// 5
func addCoins() {
    println("added 50 coins")
}

// 6
func RestorePurchases() {
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

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

// 2
func buyProduct() {
    println("buy " + p.productIdentifier)
    var pay = SKPayment(product: p)
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}

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

// 4
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 "bundle id":
            println("remove ads")
            removeAds()
        case "bundleid":
            println("add coins to account")
            addCoins()
        default:
            println("IAP not setup")
        }

    }
}

// 5
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:
            println("buy, ok unlock iap here")
            println(p.productIdentifier)

            let prodID = p.productIdentifier as String
            switch prodID {
                case "bundle id":
                    println("remove ads")
                    removeAds()
                case "bundle id":
                    println("add coins to account")
                    addCoins()
                default:
                    println("IAP not setup")
            }

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

        }
    }
}

// 6
func finishTransaction(trans:SKPaymentTransaction)
{
    println("finish trans")
}

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

You would call the removeAdsButton() or whatever when you want to make a purchase.