È possibile implementare un acquisto in-app all'interno dello SKScene
? Se é cosi, come? Sto cercando di usare un SKSpriteNode
come pulsante 'Acquista' senza fortuna. Non sono sicuro che il codice debba essere inserito nel SKScene
o nel controller di visualizzazione. Ho esaminato un sacco di tutorial, ma sembrano tutti mirati alle applicazioni a vista singola piuttosto che a SpriteKit.in acquisto di app in SKScene
6
A
risposta
3
In primo luogo, mettere questo nella vostra linea scena di gioco e assicurarsi di avere il 'StoreKit' quadro importati
class GameScene: SKScene, SKPaymentTransactionObserver, SKProductsRequestDelegate {
Avanti, la vostra intenzione di voler mettere queste righe nel vostro didmovetoview. Tieni presente che dopo gli "oggetti", la stringa che hai inserito deve essere l'identificatore di acquisto dell'app che hai configurato utilizzando iTunes connect.
// Set IAPS
if(SKPaymentQueue.canMakePayments()) {
println("IAP is enabled, loading")
var productID:NSSet = NSSet(objects: "Put IAP id here")
var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
request.delegate = self
request.start()
} else {
println("please enable IAPS")
}
Al di fuori di qualsiasi altra funzione, ma ancora all'interno della scena di gioco, inserire queste funzioni e variabili
//In App Purchases
var list = [SKProduct]()
var p = SKProduct()
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 "IAP id here":
//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 "IAP id here":
//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");
}
Poi si deve nominare il nodo è necessario fare l'IAP
whateverYourNodeIs.name = "inAppPurchaseNode"
Infine, fatelo al contattoBegan
let touch = touches.first as? UITouch
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name {
if name == "inAppPurchaseNode" {
for product in list {
var prodID = product.productIdentifier
if(prodID == "iAp id here") {
p = product
buyProduct() //This is one of the functions we added earlier
break;
}
}
}
}
Si vorrà anche questo nei tuoi tocchi ha iniziato a ripristinare gli acquisti utilizzando un nodo diverso.
if let name = touchedNode.name {
if name == "restore" {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
}
Ovunque ho messo la stringa "IAP id qui" intendo che è necessario utilizzare l'in app purchase id che hai fatto in iTunes Connect –
Grazie mille per il vostro aiuto, ho cercato ovunque per una risposta come questo !! :) :) – JGrn84
Sei il benvenuto. Inoltre, ho notato che ho lasciato alcune UIAlertViews nel codice di un gioco che ho fatto qualche tempo fa. Puoi eliminarli o cambiare il testo se lo desideri. –