How do you add an in-app purchase to an iOS application?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Adding in-app purchases to an iOS app has two parts: product configuration in App Store Connect and transaction handling in the app. The modern iOS implementation uses StoreKit to fetch products, initiate purchases, verify transactions, and unlock entitlements in a way that is safe to repeat and restore.
Configure Products in App Store Connect
Before writing app code, define the products in App Store Connect. At a minimum, you need:
- an app record
- the In-App Purchase capability enabled for the app target
- one or more product identifiers
- product metadata such as pricing and localization
The product IDs you create there must exactly match the IDs your code requests later.
Fetch Products with StoreKit
A good starting point is a store service that loads the product definitions available to the app.
This gives you Product values you can display in the UI with localized pricing and names.
Start the Purchase Flow
Once you have a Product, call purchase() and inspect the result carefully.
The verification step matters. You should not unlock content just because a purchase attempt occurred.
Restore and Observe Transactions
Users expect paid content to restore correctly across reinstalls and devices. You also need to handle transaction updates that arrive outside the exact moment the purchase button was tapped.
For manual restore flows, call:
That asks the system to re-sync purchases for the signed-in App Store account.
Model Entitlements Explicitly
Your app should separate payment events from access control. A transaction indicates that StoreKit recorded a purchase. An entitlement is your app's decision about what the user may use.
In production systems, durable entitlement storage and server-side validation are often preferable to simple local flags, especially for subscriptions and cross-device access.
Test with Sandbox and StoreKit Configuration
Do not test purchases only in production-like conditions. Use StoreKit testing support and sandbox accounts so you can repeat purchase flows, cancellation paths, and restore behavior safely.
This is where many bugs surface: not in the purchase button itself, but in entitlement restoration, pending purchases, and repeated app launches.
Common Pitfalls
A common mistake is hardcoding UI assumptions before the products are loaded. Product metadata should come from StoreKit, not from guessed pricing strings in the app.
Another is unlocking features without checking transaction verification. That creates a security hole and also makes debugging harder.
Developers also often forget to handle transaction updates and restore flows, which leads to users who paid successfully but do not regain access later.
Summary
- Configure product identifiers in App Store Connect before writing app logic.
- Fetch products with StoreKit and display the returned metadata.
- Start purchases through
purchase()and verify transactions before unlocking content. - Observe transaction updates and support restore flows with
AppStore.sync(). - Treat entitlements as a deliberate app-level model, not just as a button press result.
Related reading
- How do you add multi-line text to a UIButton?
- How do you beta test an iphone app?
- How do you change text to bold in Android?
- How do you change the launcher logo of an app in Android Studio?
- How do you check current view controller class in Swift?
- How do you check if SwiftUI is in preview mode?
- How do you convert an iPhone OSStatus code to something useful?
- How do you create a Swift Date object?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.