Monetizing your application is a crucial step for many developers.
Apple provides the StoreKit framework to handle secure financial transactions directly within your app.
With the recent introduction of StoreKit 2, managing products, purchases, and user entitlements in Swift has never been easier or safer.
Before writing code, you must define your products in App Store Connect. There are four main types:
To display products to your user, you must fetch the localized prices and descriptions directly from Apple's servers using the Product IDs you defined in App Store Connect.
import StoreKitfunc fetchProUpgrade() async -> Product? { do { // Provide the exact Product ID string from App Store Connect let products = try await Product.products(for: ["com.intricatedevo.app.promode"]) return products.first } catch { print("Failed to fetch product from Apple: (error)") return nil } }
When the user taps a "Buy" button, you initiate the purchase flow.
StoreKit takes over, authenticates the user with Face ID or Touch ID, and processes the payment securely.
import StoreKitfunc purchase(product: Product) async { do { let result = try await product.purchase() switch result { case .success(let verification): // The purchase was successful! // StoreKit 2 automatically handles cryptographic receipt validation for you. print("Purchase successful!") case .userCancelled: print("User cancelled the purchase prompt.") case .pending: print("Purchase is pending (e.g., waiting for parental approval).") @unknown default: break } } catch { print("Purchase failed with error: (error)") } }
For non-consumables and subscriptions, you must continuously check if the user is entitled to the premium features every time they open the app.
With StoreKit 2, you simply iterate over Transaction.currentEntitlements to unlock the app's features automatically.
Which type of In-App Purchase is best suited for selling an ad-free "Pro Mode" that the user only pays for once?