在SKScene的应用程序购买

是否可以在SKScene中实现应用内购买? 如果是这样,怎么样? 我试图使用SKSpriteNode作为'购买'按钮,但没有运气。 我不确定代码是否需要进入SKScene或视图控制器。 我看过大量的教程,但它们似乎都是针对单一视图应用程序,而不是SpriteKit。


首先,把它放在你的游戏场景中,并确保你已经导入了框架'StoreKit'

class GameScene: SKScene, SKPaymentTransactionObserver, SKProductsRequestDelegate {

接下来,您将要将这些行放入您的didmovetoview中。 请记住,在“objects:”之后,您放置的字符串应该是使用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")
    }

除了任何其他功能,但仍然在游戏场景中,插入这些函数和变量

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

接下来,您必须命名您需要执行iAP的节点

whateverYourNodeIs.name = "inAppPurchaseNode"

最后,在touchesBegan中做这个

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

    }

你也会希望在你的触摸中开始恢复使用不同节点的购买。

    if let name = touchedNode.name {
        if name == "restore" {

             SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
            SKPaymentQueue.defaultQueue().addTransactionObserver(self)
             }
        }
链接地址: http://www.djcxy.com/p/87345.html

上一篇: in app purchase in SKScene

下一篇: self.window is always nil