在 SKScene 中的应用内购买 [英] in app purchase in SKScene

查看:14
本文介绍了在 SKScene 中的应用内购买的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 SKScene 中实现应用内购买?如果是这样,怎么做?我正在尝试使用 SKSpriteNode 作为购买"按钮,但没有成功.我不确定代码是否需要进入 SKScene 或视图控制器.我看过很多教程,但它们似乎都是针对单视图应用程序而不是 SpriteKit.

Is it possible to implement an in-app purchase within the SKScene? If so, how? I'm trying to use a SKSpriteNode as a 'buy' button with no luck. I'm not sure whether the code needs to go in the SKScene or the view controller. I've looked at loads of tutorials, but they all seem to be aimed at single view applications rather than in SpriteKit.

推荐答案

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

First, put this in your game scene line and make sure you have the framework 'StoreKit' imported

class GameScene: SKScene, SKPaymentTransactionObserver, SKProductsRequestDelegate {

接下来,您需要将这些行放入您的 didmovetoview 中.请记住,在objects:"之后,您输入的字符串应该是您使用 iTunes connect 设置的应用内购买标识符.

Next, your going to want to put these lines in your didmovetoview. Keep in mind that after the "objects:" The string you put should be the in app purchase identifier you set up using 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")
    }

在任何其他函数之外,但仍在游戏场景中,插入这些函数和变量

Outside of any other functions, but still within the game scene, insert these functions and variables

//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 所需的节点

Next you must name the node you need to do the iAP

whateverYourNodeIs.name = "inAppPurchaseNode"

最后,在 touchesBegan 中执行此操作

Finally, do this in the 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;
                    }
                }
            }

    }

您还希望在您的触摸中开始使用不同的节点恢复购买.

You will also want this in your touches began to restore the purchases using a different node.

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

             SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
            SKPaymentQueue.defaultQueue().addTransactionObserver(self)
             }
        }

这篇关于在 SKScene 中的应用内购买的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆