1

所以我知道问题出在带有 IAP 的 ViewController 上,但不确定如何解决。

我添加SKPaymentQueue.defaultQueue().addTransactionObserver(self)viewDidLoad有班级的代表;SKProductsRequestDelegate, SKPaymentTransactionObserver.

我使用这些方法来执行购买交易、恢复任何购买并进行非消耗性购买所需的任何更改。

但无论用户做什么,加载IAPViewController应用程序后都会不断提示用户登录。它主要是当用户退出应用程序然后重新打开它时。

我尝试SKPaymentQueue.defaultQueue().removeTransactionObserver(self)在关闭视图时添加,但这似乎不起作用。

有没有人有什么建议 ??由于这个错误,我不得不将我的应用程序从审查中撤出:(

这是完整的代码;

class IAPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

var productIDs: Array<String!> = []
var productsArray: Array<SKProduct!> = []

var selectedProductIndex: Int!
var transactionInProgress = false

override func viewDidLoad() {
    super.viewDidLoad()


    productIDs.append("com.COMPANY.NAME.BUY01")
    productIDs.append("com.COMPANY.NAME.BUY02")
    productIDs.append("com.COMPANY.NAME.BUY03")
    productIDs.append("com.COMPANY.NAME.BUY04")

    requestProductInfo()
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
 }

@IBAction func purchase04(sender: AnyObject) {

    selectedProductIndex = 0
    transactionAction()


}

@IBAction func restoreAction(sender: AnyObject) {
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}


    //StoreKit


    func transactionAction() {
        let payment = SKPayment(product: self.productsArray[self.selectedProductIndex] as SKProduct)
        SKPaymentQueue.defaultQueue().addPayment(payment)
        self.transactionInProgress = true

    }

    //Request Products
    func requestProductInfo() {
        if SKPaymentQueue.canMakePayments() {
            let productIdentifiers = NSSet(array: productIDs)
            let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)

            productRequest.delegate = self
            productRequest.start()

        }
        else {
            print("Cannot perform In App Purchases.")
        }

    }

    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
        if response.products.count != 0 {
            for product in response.products {
                print("\(product.localizedTitle)")
                productsArray.append(product)
            }
        }
        else {
            print("There are no products.")
        }

        if response.invalidProductIdentifiers.count != 0 {
            print("\(response.invalidProductIdentifiers.description)")
        }

        print("Number of products in productsArray \(productsArray.count) - Number of products in productIDs \(productIDs.count)")

}


    //Payment Observer
    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case SKPaymentTransactionState.Purchased:
                print("Transaction completed successfully.", terminator: "")
                self.deliverProduct(transaction)
                SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                transactionInProgress = false

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

            default:
                print(transaction.transactionState.rawValue, terminator: "")
            }
        }
    }

    func deliverProduct(transaction:SKPaymentTransaction) {

        if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY01"
        {
            print("Consumable Product 1 Purchased")

        }
        else if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY02"
        {
            print("Consumable Product 2 Purchased")
        }
        else if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY03"
        {
            print("Product 3 purchased")
        }
        else if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY04"
        {
            print("Product 4 purchased")
        }

    }

//Restore Purchases

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
    print("Transactions Restored")

    let purchasedItemIDS = []


    for transaction:SKPaymentTransaction in queue.transactions {

        if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY04"
        {
            print("Consumable Product Purchased")

        }

    }

    let alertController = UIAlertController(title: "Restored Purchases", message: "Your purchases have been restored. ", preferredStyle: .Alert)

    let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
        // ...
    }
    alertController.addAction(OKAction)

    self.presentViewController(alertController, animated: true) {
        // ...
    }

}

@IBAction func exit(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)

        SKPaymentQueue.defaultQueue().removeTransactionObserver(self)

}

}
4

1 回答 1

0

听起来您没有finishTransaction按照此处的文档调用:

交易一直在支付队列中,直到它们被删除。StoreKit 将调用您的观察者的 paymentQueue: updatedTransactions: 每次您的应用程序从后台启动或恢复时,直到它们被删除。为此,您的客户可能会被反复要求对他们的购买进行身份验证或被阻止购买您的产品。

在您的事务上调用 finishTransaction: 以将其从队列中删除。已完成的事务不可恢复。因此,请务必提供您的内容,下载产品的所有 Apple 托管内容,或在完成交易之前完成您的购买流程。有关更多信息,请参阅完成事务。

你知道这两个调用之一finishTransaction实际上正在运行吗?

于 2016-02-23T06:10:46.423 回答