SpriteKit in-app purchase within SKScene

Hi

The code i found on the internet seems really good but i he didnt worked for me 😠

I dont know why this code dont work, so ask you guys for help.

Maybe you have some ideas. I mean its easy to implement a purchase to a viewcontroller but when i try to implement it to a scene i fail...

But i also cant open a viewcontroller from a scene or ? If that would work, i would be fine 😎

So if you know how to fix the code or how i can open a viewcontroller from a button, which is placed in a scene then answer please 🙂


So here is the code :


Thats the if statement which is in the didMoveToView Method :


if(SKPaymentQueue.canMakePayments()) {

println("IAP is enabled, loading")

var productID:NSSet = NSSet(objects: "smallPackCredits")

var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)

request.delegate = self

request.start()

} else {

println("please enable IAPS")

}


That are the functions :


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 "smallPackCredits":

buttonBuy.hidden = true

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 "smallPackCredits":

/

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");

}


And thats in the touchesBegan Method :


let touch = touches.first as? UITouch

let positionInScene = touch!.locationInNode(self)

let touchedNode = self.nodeAtPoint(positionInScene)

if let name = touchedNode.name {

if name == "buttonBuy" {

for product in list {

var prodID = product.productIdentifier

if(prodID == "smallPackCredits") {

p = product

buyProduct() /

break;

}

}

}

}


Thanks😍

SpriteKit in-app purchase within SKScene
 
 
Q