当我没有购买任何应用内购买时,为什么购买会恢复?

时间:2015-06-09 02:01:28

标签: ios swift in-app-purchase

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

   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;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为你的方法错了。

从Apple文档中,paymentQueueRestoreCompletedTransactionsFinished告诉观察者支付队列已完成发送恢复事务。在支付队列处理完所有可恢复的交易后调用此方法。您的申请无需采用此方法。

我想你已经实施了paymentQueue:updatedTransactions。您应该检查transactionState SKPaymentTransactionStateRestored并解锁您的级别。