为什么没有调用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::)方法中的print语句永远不会打印,设备也不会振动。看起来该方法没有被调用。
调用paymentQueue(_:updatedTransactions:)方法。执行该方法中的print语句。
在这段代码中,我为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)
}
}
2条答案
按热度按时间yzuktlbb1#
根据该方法的documentation
当用户在App Store中开始应用内购买并且交易在应用中继续时,会调用此委托方法。具体来说,如果您的应用已经安装,则调用该方法。
当用户在App Store应用中兑换应用内购买的促销代码或在App Store应用中购买促销的应用内购买时,会发生这种情况。
在您的应用中启动购买时不会调用此函数,因为您已经可以控制是否允许用户在您的应用中进行购买。
mjqavswn2#
对我很有效
override func viewDidLoad(){ super.viewDidLoad()