使用restoreCompletedTransactions()时没有得到任何响应

时间:2015-09-11 18:53:56

标签: ios swift in-app-purchase

我已在我的应用中实施应用内购买。购买工作正常,但我无法恢复购买。

按下restorepurchasenow时没有任何反应,尽管我在付款队列中实施了SKPaymentTransactionState.Restored个案。我正在使用沙盒用户来测试它,这可能是问题吗?

这是我的代码:

@IBAction func restorepurchasesnow(sender: AnyObject) {


if transactionInProgress {
    return
}

    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    restorepurchase.setTitle(NSLocalizedString("settings.inprogress", comment: "Transaction in progress"), forState: UIControlState.Normal)
    self.transactionInProgress = true
    println("Trying to restore purchases") // I get this message


}

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

    println("did receive answer from server") // this never gets fired


    for transaction in transactions as! [SKPaymentTransaction] {

        switch transaction.transactionState {

        case SKPaymentTransactionState.Purchased:
            println("Transaction completed successfully.")
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            transactionInProgress = false
            removeads.setTitle(NSLocalizedString("settings.proenabled", comment: "Pro version purchased"), forState: UIControlState.Normal)
            didpurchaseremoveads()


        case SKPaymentTransactionState.Failed:
            println("Transaction Failed");
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            transactionInProgress = false


        case SKPaymentTransactionState.Restored:
            let prodID = transaction.payment.productIdentifier as String
            println(prodID)
                if prodID == "removeads" {

                    println("Purchase restored")
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                    removeads.setTitle(NSLocalizedString("settings.proenabled", comment: "Pro version purchased"), forState: UIControlState.Normal)
                    restorepurchase.setTitle(NSLocalizedString("settings.restore", comment: "Restore purchases"), forState: UIControlState.Normal)
                    transactionInProgress = false
                    didpurchaseremoveads()

                } else {

                    transactionInProgress = false
                    println("Puchase restaured but no compatible products were found")
                    restorepurchase.setTitle(NSLocalizedString("settings.restore", comment: "Restore purchases"), forState: UIControlState.Normal)
                }

        default:
            println(transaction.transactionState.rawValue)
        }
    }
}

修改

我已实施:

   func paymentQueue(queue: SKPaymentQueue!,
        restoreCompletedTransactionsFailedWithError error: NSError!){

            println(error)
    }

我收到以下错误:Domain=SKErrorDomain Code=0 "Cannot connect to iTunes Store" UserInfo=0x17427c840 {NSLocalizedDescription=Cannot connect to iTunes Store}什么给出了?

1 个答案:

答案 0 :(得分:0)

你是在模拟器中运行吗?如果是这样,我相信您无法通过这种方式完全恢复购买。我认为它必须在设备上运行。基本上模拟器不支持StoreKit。