I ran into this problem, too. (I wanted to get the product's price upon successful purchase.) My solution is to store the each SKProduct
in a Dictionary
, using the productIdentifier
as the key. Here's the gist of my implementation (in Swift 3):
Declare the dictionary as a property in your class:
fileprivate var products = Dictionary<String, SKProduct>()
Store products in the dictionary when the productsRequest(_:didReceive:)
callback function of SKProductsRequestDelegate
is called:
extension PurchaseManager: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
for product in response.products {
products[product.productIdentifier] = product
}
}
}
When the paymentQueue(_:updatedTransactions:)
callback function of SKPaymentTransactionObserver
is called, retrieve the product from the dictionary:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
let product = products[transaction.payment.productIdentifier]
let price = product?.price
}
}