Hi, I have this code in a designated file called IAP Manager to handle all things related to in-app purchases.
And then I have this code to call it:
The problem is I can't figure out how to get the price for the location from StoreKit.
Thanks in advance!
Code Block import StoreKit import Foundation final class IAPManager: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver { static let shared = IAPManager() private var products = [SKProduct]() private var productBeingPurchased: SKProduct? enum Product: String, CaseIterable { case removeAds = "JokesRUs.RemoveAds" } public func fetchProducts() { let request = SKProductsRequest(productIdentifiers: Set(Product.allCases.compactMap(({ $0.rawValue })))) request.delegate = self request.start() } func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { products = response.products } func request(_ request: SKRequest, didFailWithError error: Error) { guard request is SKProductsRequest else { return } print("Product fetch request failed") } public func purchase(product: Product) { guard SKPaymentQueue.canMakePayments() else { return } guard let storeKitProduct = products.first(where: { $0.productIdentifier == product.rawValue }) else { return } let paymentRequest = SKPayment(product: storeKitProduct) SKPaymentQueue.default().add(self) SKPaymentQueue.default().add(paymentRequest) } func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { transactions.forEach({ transaction in switch transaction.transactionState { case .purchasing: //No op break case .purchased: handlePurchase(transaction.payment.productIdentifier) break case .failed: break case .restored: break case .deferred: break @unknown default: break } }) } private func handlePurchase(_ id: String) { UserDefaults.standard.setValue(true, forKey: id) print(id) } }
And then I have this code to call it:
Code Block IAPManager.shared.purchase(product: .removeAds)
The problem is I can't figure out how to get the price for the location from StoreKit.
Thanks in advance!