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

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

最佳答案

我认为你的方法不对。

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

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

关于ios - 为什么在我没有购买任何应用程序内购买时购买会恢复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30721881/

相关文章:

ios - 在 Swift 3.0 中访问从 iTunes API 解析的 JSON 对象

ios - 如何将 UIImage 放入 AnyObject 数组?

iphone - 检查是否选择了 UIButton

swift - UITableview 中的收藏夹按钮 (Swift 4)

ios - 应用内购买已购买

ios - 更改权限时应用程序在后台崩溃 - swift

swift - xcode TableView Controller 搜索多个数组

ios - 从其他类访问结构

iphone - 如何在 ios 中存储和获取应用内购买的响应?

ios - SwiftyStoreKit - 如何正确恢复自动更新订阅?