我一次购买一个项目没有问题,但是当我使用updatedTransactions()
快速连续地向队列中添加两个项目时,transactionState
永远不会调用第二个事务。调试队列内容显示第二个事务在队列中,.purchasing
为// AppDelegate.swift
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
let inAppPurchasesHelper = InAppPurchasesHelper()
inAppPurchasesHelper.setAsTransactionObserver()
}
}
// InAppPurchasesHelper.swift
class InAppPurchasesHelper : NSObject, SKPaymentTransactionObserver {
func setAsTransactionObserver() {
SKPaymentQueue.default().add(self)
}
func buy(productIdentifier: String) {
let product = // product retrieval from app data model using productIdentifier
SKPaymentQueue.default().add(SKPayment(product: product))
print("Added \(item.productIdentifier) to SKPaymentQueue.")
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case SKPaymentTransactionState.purchased:
print("Purchased \(transaction.payment.productIdentifier)")
downloadTransaction(transaction)
break
case SKPaymentTransactionState.failed:
print("Failed \(transaction.payment.productIdentifier)")
failedTransaction(transaction)
break
case SKPaymentTransactionState.restored:
print("Restored \(transaction.payment.productIdentifier)")
downloadTransaction(transaction)
break
case SKPaymentTransactionState.deferred:
print("Deferred \(transaction.payment.productIdentifier)")
break
case SKPaymentTransactionState.purchasing:
print("Purchasing \(transaction.payment.productIdentifier)")
break
}
}
}
func downloadTransaction(_ transaction: SKPaymentTransaction) {
if let downloads = transaction.downloads as [SKDownload]? {
SKPaymentQueue.default().start(downloads)
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
for download in downloads {
switch (download.state) {
case .waiting:
break
case .active:
break
case .finished:
finishedDownload(download)
break
case .failed:
failedDownload(download)
break
case .cancelled:
failedDownload(download)
case .paused:
break
}
}
}
}
。
相关守则:
Customize