How to Fix iOS StoreKit Testing Issues — In-App Purchases Not Working
In this tutorial, you'll learn about How to Fix iOS StoreKit Testing Issues. We cover key concepts, practical examples, and best practices.
The Problem
In-app purchases are not working:
SKProductsRequest returns no products.
or:
Payment queue does not process transactions.
Quick Fix
Step 1: Configure products in App Store Connect
App Store Connect > My Apps > Features > In-App Purchases > Create
Ensure the product ID matches your code exactly.
Step 2: Create a products request
WRONG — missing product identifiers:
let request = SKProductsRequest(productIdentifiers: [])
RIGHT:
let productIdentifiers: Set<String> = ["com.example.app.premium"]
let request = SKProductsRequest(productIdentifiers: productIdentifiers)
request.delegate = self
request.start()
Step 3: Handle the products response
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
if response.products.isEmpty {
print("No products found. Invalid product identifiers: \(response.invalidProductIdentifiers)")
}
for product in response.products {
print("Product: \(product.localizedTitle) - \(product.price)")
}
}
Step 4: Add the payment queue observer
SKPaymentQueue.default().add(self)
Step 5: Handle transactions
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction)
case .failed:
if let error = transaction.error as? SKError {
print("Purchase failed: \(error.code)")
}
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
SKPaymentQueue.default().finishTransaction(transaction)
default:
break
}
}
}
Step 6: Use StoreKit Test in Xcode 14+
Product > Scheme > Edit Scheme > Options > StoreKit Configuration > Select a configuration file
Create a .storekit file:
File > New > File > StoreKit Configuration File
Prevention
- Register products in App Store Connect before coding.
- Test with StoreKit Test configuration files.
- Handle all transaction states including
.failedand.restored.
Common Mistakes with storekit testing
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world IOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro