Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了快速 – 在SKScene购买应用程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

是否可以在SKScene内实施应用内购买?如果是这样,怎么样?我正在尝试使用SKSpriteNode作为“购买”按钮而没有运气.我不确定代码是否需要进入sKScene或视图控制器.我看过大量的教程,但它们似乎都是针对单视图应用而不是SpriteKit. 首先,将其放入游戏场景行中,并确保已导入框架“StoreKit” class GameScene: SKScene, SKPaymentTrans
是否可以在SKScene内实施应用内购买?如果是这样,怎么样?我正在尝试使用SKSpriteNode作为“购买”按钮而没有运气.我不确定代码是否需要进入sKScene或视图控制器.我看过大量的教程,但它们似乎都是针对单视图应用而不是SpriteKit.
首先,将其放入游戏场景行中,并确保已导入框架“StoreKit”
class GameScene: SKScene,SKPaymenttransactionObserver,SKProductsrequestDelegate {

接下来,您希望将这些行放在didmovetoview中.请记住,在“对象:”之后,您输入的字符串应该是您使用iTunes连接设置的应用内购买标识符.

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

大佬总结

以上是大佬教程为你收集整理的快速 – 在SKScene购买应用程序全部内容,希望文章能够帮你解决快速 – 在SKScene购买应用程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。