当我购买一个级别的应用程序内购买时,另一个级别也会解锁......为什么会发生这种情况?

When I buy an in app purchase for one level the other level unlocks too...Why does this happen?

我有这两个不同的应用程序购买,当我购买一个级别时,另一个级别也会解锁。为什么会这样?这是我拥有的代码,有点长,对此深表歉意。我想确保您可以查看代码是否有任何问题。谢谢!

// IAP functions

func unlockLevel() {
    if SKPaymentQueue.canMakePayments() {
        var productID:NSSet = NSSet(object: "unlockLevelTwo")
        var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        productsRequest.delegate = self
        productsRequest.start()
        println("Fetching products...")
    }
    else {
        println("Can't make purchases")
    }
}


func unlockLevel2() {
    if SKPaymentQueue.canMakePayments() {
        var productID:NSSet = NSSet(object: "unlockLevelThree")
        var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        productsRequest.delegate = self
        productsRequest.start()
        println("Fetching products...")
    }
    else {
        println("Can't make purchases")
    }
}



func buyProduct(product: SKProduct) {
    println("Sending the Payment Request to Apple");
    var payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
}

func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
    var count : Int = response.products.count
    if (count>0) {
        var validProducts = response.products
        var validProduct: SKProduct = response.products[0] as! SKProduct
        if (validProduct.productIdentifier == "unlockLevelTwo") {
            println(validProduct.localizedTitle)
            println(validProduct.localizedDescription)
            println(validProduct.price)
            buyProduct(validProduct);
        }
        if (validProduct.productIdentifier == "unlockLevelThree") {
            println(validProduct.localizedTitle)
            println(validProduct.localizedDescription)
            println(validProduct.price)
            buyProduct(validProduct);
        }


        else {
            println(validProduct.productIdentifier)
        }
    }
    else {
        println("nothing")
    }
}

func request(request: SKRequest!, didFailWithError error: NSError!) {
    println("Error fetching product info")
}

func RestorePurchases() {
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()


}

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {

    for item in queue.transactions {
        if let transaction = item as? SKPaymentTransaction {
            if transaction.transactionState == .Restored {
                unlockLevel()
                unlockLevel2()
                SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                break
            }
        }
    }
    if queue.transactions.count == 0 {
        println("not restored")
        displayRestoreAlert()
    }
}

func displayRestoreAlert() {

    let alertController = UIAlertController(title: "Level Two has not been purchased on this account.", message: nil, preferredStyle: .Alert)
    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(OKAction)
    self.viewController.presentViewController(alertController, animated: true, completion: nil)

}



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

    for transaction:AnyObject in transactions {
        if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
            switch trans.transactionState {
            case .Purchased:
                println("Product Purchased");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                NSUserDefaults().setBool(true, forKey: "Leavel2")
                unlockLevelTwo()
                unlockLevelTwoImage.removeFromParent()
                unlockLevelTwoButton.removeFromParent()

                NSUserDefaults().setBool(true, forKey: "Leavel3")
                unlockLevelThree()
                unlockLevelThreeImage.removeFromParent()
                unlockLevelThreeButton.removeFromParent()

                break;
            case .Failed:
                println("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;
            case .Restored:
                println("Already Purchased");
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

            default:
                break;
            }
        }
    }
}

我认为您需要稍微检查一下您的应用程序逻辑。

for transaction:AnyObject in transactions {
    if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
        switch trans.transactionState {
        case .Purchased:
            println("Product Purchased");
            SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
            NSUserDefaults().setBool(true, forKey: "Leavel2")
            unlockLevelTwo() // <---- Unlock
            unlockLevelTwoImage.removeFromParent()
            unlockLevelTwoButton.removeFromParent()

            NSUserDefaults().setBool(true, forKey: "Leavel3")
            unlockLevelThree()  // <---- Unlock
            unlockLevelThreeImage.removeFromParent()
            unlockLevelThreeButton.removeFromParent()

            break;
        case .Failed:
            println("Purchased Failed");
            SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
            break;
        case .Restored:
            println("Already Purchased");
            SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

        default:
            break;
        }
    }
}

如果有 .Purchased,代码将解锁两个级别。

您的还原也是如此:

for item in queue.transactions {
    if let transaction = item as? SKPaymentTransaction {
        if transaction.transactionState == .Restored {
            unlockLevel() // <---- Unlock
            unlockLevel2() // <---- Unlock
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            break
        }
    }
}

请查看 SKPaymentTransaction 的文档。

您似乎需要检查 SKPaymentTransaction.downloads NSArray,它是 "An array of download objects representing the downloadable content associated with the transaction. (read-only)"