Swift In-App-Purchase:持续提示登录;错误处理.finishTransaction?

时间:2016-03-10 13:38:13

标签: ios swift swift2 in-app-purchase skpaymenttransaction

我包含了许多非耗材IAP,但是在加载我的应用时遇到了问题,因为它似乎经常提示我登录我设置的测试帐户用户。

我在看了类似的问题之后,我相信这是因为我最初未能在每个.case中添加以下行:

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

从下面的代码中可以看出,这是添加到每个.case中的。要重新测试,我创建了一个额外的测试帐户,但我现在得到了完全相同的结果(在每个viewDidLoad上,我被提示登录到2个测试帐户......)。

这让我觉得SKPaymentQueue.defaultQueue()。finishTransaction(交易为!SKPaymentTransaction)没有按预期工作?

任何帮助都将不胜感激。

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

其次是

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
    }

}

我遇到了这个问题,因为finishTransaction没有按预期工作,或原始交易变得“卡住”?

修改 这是我称之为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")
    }
}

1 个答案:

答案 0 :(得分:0)

这已经“固定”了。我认为现在我已添加:

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

每次我重新登录时,它都会完成以前“卡住”的交易,并将其从积压中移除?

无论哪种方式;现在已经解决了。