StoreKit for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is StoreKit and why is it needed?

\n\n

StoreKit is a built-in framework from Apple designed for implementing In-App Purchases and subscriptions in apps for iOS, macOS, watchOS, and tvOS. It provides developers with a unified interface for interacting with the App Store: requesting products, processing transactions, verifying access rights, and managing subscriptions.

\n\n

Main tasks that StoreKit solves:

\n
    \n
  • Monetization — selling digital goods (premium access, virtual currency, content).
  • \n
  • Subscription management — automatic renewals, statuses, trial periods.
  • \n
  • Security — processing payments through the App Store without transferring bank card data to the developer.
  • \n
  • Purchase restoration — users can transfer purchases to another device.
  • \n
\n\n

It is important to understand: StoreKit is not a third-party library, but part of the Xcode SDK. It does not need to be installed separately, but it must be configured in App Store Connect and added to the project.

\n\n

Installation and Setup

\n\n

Since StoreKit is a system framework, its installation comes down to connecting it in the Xcode project:

\n\n
    \n
  1. Open your project in Xcode.
  2. \n
  3. Select the target (your app) → "General" tab.
  4. \n
  5. Scroll to the "Frameworks, Libraries, and Embedded Content" section.
  6. \n
  7. Click "+" and select StoreKit.framework.
  8. \n
  9. Make sure the "Embed" status is set to "Do Not Embed".
  10. \n
\n\n

After that, import the framework in the necessary files:

\n\n
import StoreKit
\n\n

Also, be sure to configure products in App Store Connect:

\n
    \n
  • Create product identifiers (e.g., com.example.premium).
  • \n
  • Specify price, localization, type (consumable/non-consumable/subscription).
  • \n
  • For subscriptions, configure groups and trial periods.
  • \n
\n\n

Quick Start with Code Example

\n\n

Minimal working example for requesting and purchasing a product:

\n\n
import StoreKit\n\nclass StoreManager: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver {\n    \n    static let shared = StoreManager()\n    private var products: [SKProduct] = []\n    \n    override private init() {\n        super.init()\n        SKPaymentQueue.default().add(self)\n    }\n    \n    // 1. Request products\n    func fetchProducts(productIDs: Set<String>) {\n        let request = SKProductsRequest(productIdentifiers: productIDs)\n        request.delegate = self\n        request.start()\n    }\n    \n    // 2. Delegate for receiving products\n    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {\n        self.products = response.products\n        for product in products {\n            print("Product found: \\(product.localizedTitle) — \\(product.price)")\n        }\n    }\n    \n    // 3. Purchase\n    func purchase(product: SKProduct) {\n        let payment = SKPayment(product: product)\n        SKPaymentQueue.default().add(payment)\n    }\n    \n    // 4. Transaction handling\n    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {\n        for transaction in transactions {\n            switch transaction.transactionState {\n            case .purchased:\n                // Unlock content\n                print("Purchase successful: \\(transaction.payment.productIdentifier)")\n                SKPaymentQueue.default().finishTransaction(transaction)\n            case .failed:\n                if let error = transaction.error {\n                    print("Purchase error: \\(error.localizedDescription)")\n                }\n                SKPaymentQueue.default().finishTransaction(transaction)\n            case .restored:\n                print("Purch                    

Recommendations