Swift,spritekit:在应用程序购买代码运行中,没有发生? [英] Swift, spritekit: In app purchase code runs, NOTHING happens?

查看:111
本文介绍了Swift,spritekit:在应用程序购买代码运行中,没有发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在Swift工作,我只需要帮助。我已经关注了如何使用Swift在sprite工具包中实现应用程序购买的4个不同的教程,逐字复制的代码,没有任何对我有用。

Ok, so I'm working in Swift and I just need help. I have followed 4 different tutorials on how to implement in app purchases in sprite kit with Swift, copied code verbatim, and nothing is working for me.

以下是我采取的步骤:


  1. 进入Itunes Connect并在我的应用程序记录下创建了一个应用内购买。我在应用内购买的产品ID是GameOverSaveSavior

在Xcode中,我已经开启了我的应用在 ON 的应用购买功能中,确保我的团队设置为我的帐户,并确保我的信息下的捆绑标识符设置为我的应用程序在ITunes Connect中的捆绑标识符

In Xcode, I've turned my app's In app purchase capability to ON, made sure my team is set to my account, and made sure my Bundle Identifier under info is set to my app's bundle identifier in ITunes Connect

在编写任何代码之前,我在我的GameScene.swift文件中有 import StoreKit

Before writing any code, I have import StoreKit in my GameScene.swift file

至于代码,这就是我所做的:

As for code, this is what I have done:

(1)在Gamescene.swift中,在我的 didMoveToView func的末尾,我有:

(1) In Gamescene.swift, at the end of my didMoveToView func, I have:

// Set IAPS
        if(SKPaymentQueue.canMakePayments()) {
            println("IAP is enabled, loading")
            var productID:NSSet = NSSet(objects: "GameOverSaveSavior")
            var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
            request.delegate = self
            request.start()
        } else {
            println("please enable IAPS")
        }

当应用程序运行时,输出IAP已启用,加载

This outputs "IAP is enabled, loading" when the app is run.

(2)在GameScene.swift中,在类中,但在 didMoveToView 之外,我有所有的功能和其他用于应用内购买的变量:

(2) In GameScene.swift, within the class but outside of didMoveToView, I have all the functions and variables others have used for in app purchases:

var list = [SKProduct]()
var p = SKProduct()

    func purchaseMade() {
        println("they bought it!")
    }

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

                purchaseMade()

                //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 "GameOverSaveSavior":

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

此输出产品请求到控制台。

(3)在GameScene.swift中,在我的 touchesBegan func中,我在右键的时候有以下内容被触及:

(3) In GameScene.swift, in my touchesBegan func, I have the following for when the right button is touched:

//In app purchase
            if touchedNode == saveMeBtn {

                println("button touched!")
                for product in list {
                    var prodID = product.productIdentifier
                    if(prodID == "GameOverSaveSavior") {
                        p = product
                        buyProduct()  //This is one of the functions we added earlier
                        break;
                    }
                }

此输出触摸按钮!触摸按钮时。

我不知道我做错了什么。代码编译时没有错误,但触摸我的按钮时没有任何反应。没有警告要求用户登录或购买任何东西。
我主要关注这个问题:
在应用内购买在SKScene中

I do not know what I am doing wrong. The code compiles with no errors, yet nothing occurs when my button is touched. There is no alert asking the user to sign in or to purchase anything- nothing. I have mainly followed this question: in app purchase in SKScene

在编写代码之前还有什么我应该做的吗?我在代码中遗漏了什么吗?

Is there anything else I should have done prior to writing the code? Am I missing something in my code?

推荐答案

我想我可以帮助你:

过去我遇到了你的问题。还有一个类似的我在这里修复过的:我的IAP无效。 func Paymentqueue的错误

I had your issue in the past. And another similar one which I fixed here: My IAP isn't working. Bugs at func Paymentqueue

这是我找到的解决方案:

Here is the solution I had found:

删除

SKPaymentQueue.defaultQueue().addTransactionObserver(self) 

你到处都有它并且只在你每次启动应用程序时执行一次(仅限一次)(我把它放在viewDidLoad()中)。

everywhere you have it and put it once (ONLY ONCE) in a place where it will be executed each time your app boots up ( I put it in viewDidLoad()).

这将检查所有未完成的交易,并在应用程序加载后终止它们,从而在用户触发IAP之前删除任何可能的错误。

This will check for all unfinished transactions and terminate them once the app has loaded, thus removing any possible errors before your users triggers an IAP.

(如果这个答案有所帮助,请不要忘记upvote;))

(If this answer helped you, don't forget to upvote ;))

PS:此外,这不是我的问题,但要确保每个PurchaseState的finishTransaction(),如下所示:

P.S.: Also, this wasn't my issue, but make sure to finishTransaction() for each PurchaseState, like here:

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        print("Add Payment")

        for transaction:AnyObject in transactions{
            let trans = transaction as! SKPaymentTransaction
            print(trans.error)
            switch trans.transactionState{
            case .Purchased:
                print("IAP unlocked")
                print(p.productIdentifier)

                let prodID = p.productIdentifier as String
                switch prodID{
                case "IAP id":
                    print("Keep on")
                    keepOn()
                default:
                    print("IAP not setup")
                }
                queue.finishTransaction(trans)
                break
            case .Failed:
                print ("Buy error")
                queue.finishTransaction(trans)
                break
            default:
                print("default: Error")
                break
            }
        }
    }

永远不要忘记这一点:

 queue.finishTransaction(trans)

并为.Restored单独设置案例。

And make separate case for .Restored.

这篇关于Swift,spritekit:在应用程序购买代码运行中,没有发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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