iosswiftswift2in-app-purchaseskpaymenttransaction

Swift In-App-Purchase: Constant prompt to sign in; incorrectly handling .finishTransaction?


I include a number of non consumable IAPs but I'm encountering issues when loading my app up as it seems to constantly prompt me to log in to my test account user I setup.

I after looking at similar questions, I believe this was because I had originally failed to add the following line to each .case:

 SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 

As you can see from my below code, this is added to each .case. To re-test I created an additional test account, but I'm now getting the exact same results (on each viewDidLoad I am being prompted to log into 2 test accounts...).

Which makes me think that SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) is not working as expected?

Any help would be appreciated.

override func viewDidLoad() {
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}

Followed by

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])    {
    print("received response ok");
     if product_id == "xxxxxxxxx" {
        for transaction:AnyObject in transactions {
            if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
                switch trans.transactionState {
                case .Purchased:
                    print("Product Purchased");
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                    defaults.setBool(true , forKey: "Purchased")
                    print("Set key ok")
                    break;
                case .Failed:
                    print("Purchased Failed");
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                    break;
                case .Restored:
                    print("Already Purchased");
                    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                default:
                    break;
                }
            }
        }
    }
    else {
        //blah blah
    }

}

I am encountering this issue due to the finishTransaction not working as expected, or have the original transaction become "stuck" ?

EDIT: Here's where I call SKPaymentQueue.defaultQueue().addPayment(payment);

func buyProduct(product: SKProduct){
    print("Sending the Payment Request to Apple");
    let payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);

}


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

    let count : Int = response.products.count
    if (count>0) {
        var validProducts = response.products
        let validProduct: SKProduct = response.products[0] as SKProduct
        if (validProduct.productIdentifier == self.product_id) {
            print(validProduct.localizedTitle)
            print(validProduct.localizedDescription)
            print(validProduct.price)
            buyProduct(validProduct);
        } else {
            print(validProduct.productIdentifier)
        }
    } else {
        print("nothing")
    }
}

Solution

  • This has "fixed itself". I presume that now I had added:

    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
    

    each time I was re-logging in, it was finishing the previously "stuck" transactions, and removing them from the backlog?

    Either way; it's resolved now.