在使用SKPaymentTransactionObserver打开视图后,应用程序一直要求输入Apple ID

时间:2016-02-23 01:23:53

标签: ios swift storekit skpaymenttransaction

所以我知道问题是带有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)

}

}

1 个答案:

答案 0 :(得分:0)

听起来您没有根据文档here调用finishTransaction

  

交易保留在付款队列中,直到删除为止。   StoreKit将调用您的观察者的paymentQueue:updatedTransactions:   每次您的应用程序启动或从后台恢复,直到   他们被删除了。为此,您的客户可能会反复出现   要求验证他们的购买或被禁止购买   你的产品。

     

在您的交易上调用finishTransaction:将其从中删除   队列。已完成的交易无法收回。因此,请务必   提供您的内容,下载所有Apple托管的内容   产品,或在完成您的购买前完成您的购买流程   交易。有关详细信息,请参阅完成交易。

您知道对finishTransaction的两次调用之一实际上正在运行吗?