Production-grade Swift 6 & SwiftUI StoreKit 2 monetization, A/B paywall experimentation, and offline-first Keychain entitlement architecture extracted from commercial App Store apps.
📚 Interactive API Reference: SwiftStoreFlow DocC Documentation
flowchart LR
UI["SwiftUI PaywallView<br/>Standard · Trial Timeline · Feature Matrix"] --> VM["@MainActor PaywallViewModel<br/>(@Published State Machine)"]
AB["ExperimentBucketer<br/>FNV-1a Deterministic A/B"] --> VM
VM --> C["actor EntitlementCoordinator<br/>(Purchase Deduplication)"]
C --> SK["StoreClientProtocol<br/>StoreKit 2 / Mock"]
C --> KC["EntitlementCacheProtocol<br/>Keychain / InMemory Actor"]
OBS["actor TransactionObserverActor<br/>AsyncStream + JWS Validator"] --> KC
OBS --> VM
C & VM --> AN["PaywallAnalyticsProtocol<br/>Funnel & Exposure Events"]
- Rich Subscription Lifecycle Models (
SubscriptionLifecycle.swift): ModelsRenewalState(subscribed,inGracePeriod,inBillingRetry,expired,revoked),PromotionalOfferMetadata(introductory trials, win-back offers),SubscriptionReceiptSnapshot(JWS), andRevenueMetricsSnapshot. - Deterministic A/B Paywall Engine (
ExperimentBucketer.swift): Stateless 64-bit FNV-1a bucketing with weighted variants, holdout cohorts, QA/TestFlight overrides, and automatic exposure tracking. - Live Transaction Stream & JWS Validation (
TransactionObserverActor.swift): Multi-consumerAsyncStream<EntitlementUpdateEvent>bridging StoreKit 2 updates through a pluggableReceiptValidatorProtocolwith.strictOnlineor.allowCachedGraceWindowoffline policies. - 3 Adaptive SwiftUI Paywall Templates:
StandardCards,TrialTimelineView(Day 1 → Reminder → Billing), andFeatureComparisonMatrixView+ animatedPaywallSkeletonViewand billing-retry alert banner. - Churn Recovery & Win-Back Engine (
WinbackOfferEngine.swift,PaywallAnalyticsInspectorView.swift): Deterministic offer recommendation engine for auto-renew cancellations, billing retry bridges, and recent vs. deep lapsed subscribers, paired with a Chi-Square Sample Ratio Mismatch (SRM) guard and SwiftUI A/B telemetry overlay. - Strict Swift 6 Concurrency: Full
Sendablevalue types, actor-isolated purchase deduplication, Keychain storage (kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly), and zero data-race warnings.
import SwiftUI
import SwiftStoreFlow
let experiment = PaywallExperiment(
experimentID: "onboarding_paywall_v2",
variants: [
PaywallExperimentVariant(id: "control", weight: 50, templateStyle: .standardCards),
PaywallExperimentVariant(id: "timeline", weight: 50, templateStyle: .trialTimeline, headline: "Try 7 Days Free")
],
holdoutPercentage: 0.10
)
@MainActor
struct AppRootView: View {
@StateObject private var viewModel = PaywallViewModel(
coordinator: EntitlementCoordinator(
client: StoreKitClient(tierMapping: ["app.pro.yearly": .pro]),
cache: KeychainEntitlementCache()
),
productIDs: ["app.pro.monthly", "app.pro.yearly"],
experiment: experiment,
userID: "user-12345"
)
var body: some View {
ProWorkspaceView()
.requiresEntitlement(viewModel.entitlement, tier: .pro) {
PaywallView(viewModel: viewModel)
}
}
}26 XCTest cases covering actor-isolated purchase deduplication (12-task contention stress test), 10k-iteration FNV-1a bucketing determinism, Keychain offline entitlements, Win-Back offer rules, and Chi-Square SRM detection:
swift test --enable-code-coverageMIT — see LICENSE.