I am trying to run a function that calls my API to update user info, including the number of coins the user has after a transaction has been made. I can't figure out how to do that. Here's my code for the transaction:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
transactionState = .purchasing
print("Purchasing...")
case .purchased:
UserDefaults.standard.setValue(true, forKey: transaction.payment.productIdentifier)
print("Purchased.")
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
let receiptString = receiptData.base64EncodedString(options: [])
Task {
await postCoins(receipt: receiptString)
}
}
catch { print("Couldn't read receipt data with error: " + error.localizedDescription) }
}
transactionState = .purchased
queue.finishTransaction(transaction)
case .restored:
UserDefaults.standard.setValue(true, forKey: transaction.payment.productIdentifier)
queue.finishTransaction(transaction)
transactionState = .restored
case .failed, .deferred:
print("Payment queue error:")
print(String(describing: transaction.error))
queue.finishTransaction(transaction)
transactionState = .failed
default:
queue.finishTransaction(transaction)
}
}
}
This function is inside an SKProductsRequestDelegate
I tried making this function async by adding a completion handler and then using DispatchQueue.main.async to update the thread where the payment is made, but it throws me an error saying that does not conform the that particular protocol. It makes sense that it threw that error, but I don't know another workaround.