SKPaymentQueue.defaultQueue (). addPayment (pay as SKPayment) gives EXC_BAD_ACCESS error

0

I'm doing a game with SpriteKit and I work with SKScene for different scenes within the game.

I made a class to make purchases in-App works well with a button, but gives errors with others, I explain:

On the cover there is a button that when pressed it runs the window of buying without problems all the times that I want, but if I change to another scene and I have another buy button associated with the same kind of purchase, if I press it gives error and it falls to application.

How can I solve the problem?

This is my class to buy:

import Foundation
import StoreKit

class BuyApp: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver{

    var list = [SKProduct]()
    var p = SKProduct()
    let idEnableActividades = "id.prod.unico"
    let buy = Buy() //Base de datos con compras

    override init() {

        super.init()

        if(SKPaymentQueue.canMakePayments()) {
            print("IAP is enabled, loading")
            let productID:NSSet = NSSet(objects: self.idEnableActividades)
            let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
            request.delegate = self
            request.start()

        } else {
            print("please enable IAPS")
        }

    }

    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {

        let myProduct = response.products

        for product in myProduct {
            /*
            print(product.productIdentifier)
            print(product.localizedTitle)
            print(product.localizedDescription)
            print(product.price)
            print(product.priceLocale)
            */
            list.append(product)
        }

    }


    func buyCompleteVersion() {
        print("comprar versión completa")
        buy.showPurshases()
        if list.count > 0 {
            for product in list {
                print(product)
                let prodID = product.productIdentifier

                if(prodID == self.idEnableActividades) {
                    p = product
                    buyProduct()
                    break;
                }
            }
        } else{
            print("else de buyCompleteVersion")
        }
    }

    func saveBuy() {
        print("Guardando compra en DB.....")
        buy.savePurchase()
    }

    func buyProduct() {
        print("comprando " + p.productIdentifier)
        let pay = SKPayment(product: p)
        SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)

    }

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
        print("transactions restored")
    }



    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

        print("add paymnet")
        if transactions.count > 0 {
            for transaction:AnyObject in transactions {
                if let trans = transaction as? SKPaymentTransaction {

                    print("error: \(trans.error)")

                    switch trans.transactionState {

                    case .Purchased:

                        print(" productIdentifier: \(p.productIdentifier)")

                        let prodID = p.productIdentifier as String

                        switch prodID {
                            case self.idEnableActividades:
                                saveBuy()
                            default:
                                print("IAP not setup")
                        }

                        queue.finishTransaction(trans)
                        break;
                    case .Failed:
                        print("buy error")
                        queue.finishTransaction(trans)
                        break;
                    default:

                        print("default")
                        break;

                    }

                }
            }
        }

    }

    func finishTransaction(trans:SKPaymentTransaction)
    {
        print("finish trans")
        SKPaymentQueue.defaultQueue().finishTransaction(trans)
    }
    func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction])
    {
        if transactions.count > 0 {
            for transaction in transactions {
                SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                print("removieno")
            }
        } else{
            print("removieno 0")
        }

        print("remove trans");
    }

    func request(request: SKRequest, didFailWithError error: NSError) {
        print("“Print the NSError \(error)")
    }


}
    
asked by albertcito 06.07.2016 в 22:27
source

1 answer

0

I did the following and the same code worked for me.

  • I made a new scene for Comprar where the button is.
  • Class BuyApp leave it with the singleton pattern, therefore when starting the app I charge my products and not every time I call the class.
  • In the class Comprar I can acquire the unlock and there are no problems, I also leave an animation that says "loading" so that the user does not press the button at all times.
answered by 07.07.2016 / 00:38
source