Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Swift Basics

Swift HOME Swift Intro Swift Get Started Swift Syntax Swift Statements Swift Output Swift Comments Swift Variables Swift Data Types Swift Type Casting Swift Operators Swift Strings Swift Arrays Swift Ranges Swift If...Else Swift Switch Swift While Loop Swift For Loop Swift Break/Continue Swift Collections

Swift Types & Functions

Swift Functions Swift Optionals Swift Enums & Patterns Swift Closures Tuples & Type Aliases

Swift Object Model

Swift OOP Swift Inheritance Swift Polymorphism Swift Protocols Swift Generics Swift Extensions Access Control Initializers Deinitializers Value Semantics & COW Equatable & Comparable

Swift Robustness & Async

Swift Error Handling Swift Concurrency Swift Memory

Swift Tooling

Swift Package Manager

SwiftUI Basics

SwiftUI Intro iOS Project Setup SwiftUI Layout SwiftUI Navigation SwiftUI Data Flow SwiftUI Lists & Forms SwiftUI Animations SwiftUI Gestures SwiftUI Modifiers & ViewBuilder SwiftUI Previews SwiftUI Accessibility SwiftUI Styling & Theming

SwiftUI Data & Architecture

Networking Persistence Persistence (Core Data) MVVM Architecture AppStorage & SceneStorage Testing SwiftUI

iOS Capabilities

Privacy & Permissions Push Notifications Widgets & Extensions Background Work Core Location App Clips Keychain Basics CloudKit File System Background URLSession MapKit

iOS Quality & Compliance

Localization Accessibility App Privacy In-App Purchases Analytics & Reporting Testing with XCTest

iOS Release & Distribution

Assets & App Icons Signing & Distribution TestFlight & App Store Ship Your First App

Swift Exercises

Swift Exercises Swift Quiz

In-App Purchases (StoreKit)


In-App Purchases (StoreKit)

Purchase in-app products with StoreKit 2 by requesting products, purchasing, and verifying transactions.


Product Types

  • Consumable: Can be bought multiple times (e.g., coins).
  • Non-Consumable: One-time unlock (e.g., premium upgrade).
  • Auto-Renewable Subscription: Recurring access.

StoreKit 2 Basics

Use StoreKit 2 to request products, purchase, and verify transactions with App Store-signed receipts.

Syntax: let products = try await Product.products(for:), try await product.purchase(), verify and transaction.finish().

Example

import StoreKit

@MainActor
func buy(productID: String) async throws {
  let products = try await Product.products(for: [productID])
  guard let product = products.first else { return }
  let result = try await product.purchase()
  switch result {
  case .success(let verification):
    let transaction = try verification.payloadValue
    // unlock content
    await transaction.finish()
  default: break
  }
}

This example requests a product, performs a purchase, verifies the transaction, unlocks content, and finishes the transaction.


Fetch Products

Fetch products once (e.g., on app start) and keep them in memory for your paywall/store UI.

Example

import StoreKit

@MainActor
func loadProducts(ids: [String]) async throws -> [Product] {
  let products = try await Product.products(for: ids)
  return products.sorted { $0.displayName < $1.displayName }
}


Purchase Flow & Results

Handle all outcomes and always verify before unlocking content.

Example

import StoreKit

@MainActor
func purchase(_ product: Product) async {
  do {
    let result = try await product.purchase()
    switch result {
    case .success(let verification):
      let transaction = try verification.payloadValue
      // unlock content guarded by transaction.productID
      await transaction.finish()
    case .userCancelled:
      // show nothing or restore UI state
      break
    case .pending:
      // family approval or SCA; update UI accordingly
      break
    @unknown default:
      break
    }
  } catch {
    // network or App Store errors
  }
}

Listen for Transaction Updates

Receive purchases made outside your app UI (e.g., from the App Store or another device) and finish them.

Example

import StoreKit

func startTransactionListener() {
  Task.detached {
    for await update in Transaction.updates {
      do {
        let transaction = try update.payloadValue
        // update entitlements based on transaction.productID
        await transaction.finish()
      } catch {
        // handle verification failure
      }
    }
  }
}

Restore Purchases

Query current entitlements to restore non-consumables and active subscriptions.

Example

import StoreKit

@MainActor
func restore() async {
  for await result in Transaction.currentEntitlements {
    if let transaction = try? result.payloadValue {
      // re-unlock features for transaction.productID
    }
  }
}

Tip: Configure IAPs in App Store Connect and test with Sandbox accounts.

Use server-side receipt validation for security-sensitive unlocks.


Testing & Sandbox

Use TestFlight or the StoreKit Configuration file to simulate products; sign in with a Sandbox tester on device; note that sandbox transactions settle faster, and device logs help diagnose failures.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.