Swift StoreKit and In-App Purchases
Treat verified entitlements separately from a button tap or an unverified transaction. Requires Mac/Xcode; this Apple-only source was not SDK-compiled or run here.
How it works
Use a local StoreKit configuration or an appropriately configured sandbox to develop purchase flows. The product, account, transaction verification and entitlement policy are separate boundaries. This file only performs an explicit user-requested sync and reads verified current entitlements for a placeholder non-consumable product; it never starts a purchase.
Apple-only setup and review status
Create a separate iOS SwiftUI App learning project in a suitable Xcode version, choose an appropriate simulator/device and replace the generated app/view Swift source with this complete file. Keep one @main entry point. Widget and App Clip lessons explicitly require their own extension/clip targets instead; capability, purpose-string, entitlement and account steps in the lesson must be completed separately. No Xcode project, signing certificate, account or service was provisioned here.
Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.
Download the complete storekit.swift · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.
Example
// Apple-only learning file: iOS 17+ target with a suitable Xcode SDK.
// Not compiled or run in this Windows/Linux review.
import SwiftUI
import StoreKit
struct DemoView: View {
@State private var notice = "No entitlement check"
var body: some View {
VStack(spacing: 20) {
Button("Restore and inspect sandbox entitlements") {
Task {
do {
try await AppStore.sync()
var entitled = false
for await result in StoreKit.Transaction.currentEntitlements {
switch result {
case let .verified(transaction):
if transaction.productID == "com.example.workshop.demo", transaction.revocationDate == nil { entitled = true }
case .unverified: continue
}
}
notice = entitled ? "Verified configured demo entitlement" : "No verified demo entitlement"
} catch { notice = "Restore/check failed" }
}
}
Text(notice)
Text("No purchase is initiated by this source.").font(.caption)
}.padding()
}
}
@main
struct WorkshopDemoApp: App {
var body: some Scene { WindowGroup { DemoView() } }
}
Expected behaviour and manual checks
Manual check only in a controlled StoreKit test configuration/sandbox: inspect no entitlement, verified entitlement, unverified result, refund/revocation and account-sync failure. No purchase, account operation or entitlement verification was performed here.
A real purchase implementation must handle success verification, pending and user-cancelled outcomes, grant the correct product capability, finish appropriate transactions and monitor later updates. The read-only fixture does not claim to deliver those product workflows.
Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.