为什么在我没有购买任何应用程序内购买时购买会恢复?

Why do the purchases restore when I didn't buy any in app purchases?

我创建了一个新的沙盒测试用户来测试恢复购买按钮,但是当我按下它并输入我的密码或当我按下取消时,级别仍然解锁。我不明白为什么会这样。有人可以看看我的代码并告诉我我做错了什么吗?谢谢!

   func RestorePurchases() {
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        println("Can't make purchases")

    }



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

            println("restoreleveltwo")
            NSUserDefaults().setBool(true, forKey: "Leavel2")
            unlockLevelTwoPlease()

        case "unlockLevelThree":
            println("restorelevelthree")
            NSUserDefaults().setBool(true, forKey: "Leavel3")
            unlockLevelThreePlease()

        default:
            println("IAP not setup")
                let alert = UIAlertView()
                alert.title = "Oops"
                alert.message = "There are no purchases to restore, please buy one"
                alert.addButtonWithTitle("Cancel")
                alert.show()


        }

    }

}

//EDITED MORE CODE....

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    println("Received Payment Transaction Response from Apple");




    for transaction:AnyObject in transactions {
        var trans = transaction as! SKPaymentTransaction
        println(trans.error)


        switch trans.transactionState {



            case .Purchased:
                let prodID = p.productIdentifier as String
            switch prodID {

            case "unlockLevelTwo":
                    println("unlocksleveltwo")

                NSUserDefaults().setBool(true, forKey: "Leavel2")

                unlockLevelTwoPlease()
            case "unlockLevelThree":
                println("unlocklevelthree")
                NSUserDefaults().setBool(true, forKey: "Leavel3")
                unlockLevelThreePlease()
            default:
                println("")
                }

                queue.finishTransaction(trans)
                break;
            case .Failed:
                println("Purchased Failed");
                queue.finishTransaction(trans)
                break;
        case .Restored:
            let prodID = p.productIdentifier as String
            switch prodID {
        case "unlockLevelTwo":
            println("unlocksleveltwo")

            NSUserDefaults().setBool(true, forKey: "Leavel2")

            unlockLevelTwoPlease()
        case "unlockLevelThree":
            println("unlocklevelthree")
            NSUserDefaults().setBool(true, forKey: "Leavel3")
            unlockLevelThreePlease()
        default:
            println("")
        }

            default:
                println("default")
                break;
            }
        }
    }

我认为你的方法不对。

根据 Apple 文档,paymentQueueRestoreCompletedTransactionsFinished 告诉观察者支付队列已完成发送恢复交易。在支付队列处理完所有可恢复交易后调用此方法。您的应用程序无需在此方法中执行任何操作。

我想你已经实现了 paymentQueue:updatedTransactions。您应该检查 transactionState SKPaymentTransactionStateRestored 并在那里解锁您的关卡。