为什么未调用paymentQueue(_:shouldAddStorePayment:_ :)?

时间:2018-08-31 09:45:46

标签: ios storekit

为什么不调用paymentQueue(:shouldAddStorePayment::)?

我确定我已经完成了所有需要做的事情。

我声明了自己的类,该类支持SKPaymentTransactionObserver协议:

import UIKit
import StoreKit
import AudioToolbox.AudioServices

class UTIPaymentTransactionObserver: NSObject, SKPaymentTransactionObserver {

    func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {

        print("!!! shouldAddStorePayment")

        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)

        return false

    }

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

        print("!!! updatedTransactions")

        for transaction in transactions {
            print("!!! transaction=", transaction)
            switch transaction.transactionState {
            // Call the appropriate custom method for the transaction state.
            case SKPaymentTransactionState.purchasing:
                showTransactionAsInProgress(transaction, deferred: false)
            case SKPaymentTransactionState.deferred:
                showTransactionAsInProgress(transaction, deferred: true)
            case SKPaymentTransactionState.failed:
                failedTransaction(transaction)
            case SKPaymentTransactionState.purchased:
                completeTransaction(transaction)
            case SKPaymentTransactionState.restored:
                restoreTransaction(transaction)
            }
        }

    }

    func showTransactionAsInProgress(_ transaction: SKPaymentTransaction, deferred: Bool) {

        print("!!! showTransactionAsInProgress")

    }

    func failedTransaction(_ transaction: SKPaymentTransaction) {

        print("!!! failedTransaction")

        SKPaymentQueue.default().finishTransaction(transaction)

    }

    func completeTransaction(_ transaction: SKPaymentTransaction) {

        print("!!! completeTransaction")

        SKPaymentQueue.default().finishTransaction(transaction)

    }

    func restoreTransaction(_ transaction: SKPaymentTransaction) {

        print("!!! restoreTransaction")

    }

}

当添加paymentQueue(:shouldAddStorePayment: :)时,我添加了使设备振动的代码,以表明该方法实际上已被调用。

我全局声明了一个观察者类的实例:

internal let paymentTransactionObserver = UTIPaymentTransactionObserver()

我确保在AppDelegate中添加了观察者:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    SKPaymentQueue.default().add(paymentTransactionObserver)

    return true
}

paymentQueue(:shouldAddStorePayment: :)方法中的打印语句从不打印,设备也从不振动。看起来好像没有调用该方法。

paymentQueue(_:updatedTransactions :)方法被调用。该方法中的打印语句已执行。

在这段代码中,我为paymentQueue(:shouldAddStorePayment: :)方法返回了false,但这没有什么区别。整个过程就像我返回true一样。该产品之前已经购买过,因此可以继续使用,并允许用户/测试人员再次购买。

任何帮助将不胜感激。

这是视图控制器的代码扩展,它从App Store检索产品并显示我的用户界面,该界面允许用户购买产品:

我调用validateProductIdentifiers()来开始向用户出售产品的过程。

// MARK: - SKProductsRequestDelegate

extension CloudViewController: SKProductsRequestDelegate {

    func validateProductIdentifiers() {

        let url = Bundle.main.url(forResource: "Purchase", withExtension: "plist")!

        let nsArrayProductIdentifiers: NSArray = NSArray(contentsOf: url)!

        let productIdentifiers = nsArrayProductIdentifiers as! [String]

        print(productIdentifiers)

        let setProductIdentifers: Set = Set(productIdentifiers)

        let productsRequest = SKProductsRequest(productIdentifiers: setProductIdentifers)

        self.productsRequest = productsRequest

        productsRequest.delegate = self

        productsRequest.start()

    }

    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

        print("!!! didReceive")

        self.products = response.products

        let alertMessage = "Would you like to purchase?"

        let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .actionSheet)

        let actionYes = UIAlertAction(title: "Yes", style: .default) {

            action in

            // Purchase

            let product: SKProduct = response.products.first!

            let payment: SKMutablePayment = SKMutablePayment(product: product)
            SKPaymentQueue.default().add(payment)

        }

        let actionNo = UIAlertAction(title: "No", style: .cancel, handler: nil)

        alert.addAction(actionYes)
        alert.addAction(actionNo)

        alert.popoverPresentationController?.barButtonItem = barButtonItemEnableDropbox

        present(alert, animated: true, completion: nil)

    }

}

1 个答案:

答案 0 :(得分:0)

按照该方法的documentation

  

当用户在App Store中开始应用内购买并在您的应用中继续进行交易时,将调用此委托方法。具体来说,如果您的应用已安装,则将调用该方法。

当用户在App Store应用中为应用内购买兑换促销代码或在App Store应用中购买促销的应用内购买时,就会发生这种情况。

在您的应用中启动购买时不会调用它,因为您已经控制了用户在您的应用中时是否应允许购买。