'StoreKit purchases stuck in a loop
I am trying to implement in-app purchases in an app. this is the firs time i am doing this. I think i have the whole thing set up correctly, when i test on the device it seems to work however i get "Purchased" printed on the console a lot, it purchased 500 times (it seemed to be caught in a loop). I checked this by checking Debug -> Store Kit -> Manage transactions it seems to be purchasing in a loop. I can't see where I've gone wrong with the code.
This started happening after I added in a button that restores the purchase.
enum Product: String, CaseIterable {
case noadspremium = "unlock1"
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
if let oProduct = response.products.first {
print("product is available")
self.purchase(aproduct: oProduct)
}
else {
print("product not available")
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
print("customer is in the process of purchase")
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction)
print("purchased")
case .failed:
SKPaymentQueue.default().finishTransaction(transaction)
print("failed")
case .restored:
SKPaymentQueue.default().restoreCompletedTransactions()
print("Restored")
case .deferred:
print("deferred")
default:
break
}
}
}
func purchase(aproduct: SKProduct) {
let payment = SKPayment(product: aproduct)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(payment)
}
@IBAction func buyPremium(_ sender: Any) {
if SKPaymentQueue.canMakePayments(){
let set: Set<String> = [Product.noadspremium.rawValue]
let productRequest = SKProductsRequest(productIdentifiers: set)
productRequest.delegate = self
productRequest.start()
}
}
@IBAction func restorePressed(_ sender: Any) {
SKPaymentQueue.default().restoreCompletedTransactions()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
