5.3 KiB
5.3 KiB
KeysForAll Swift Package
A flexible, reusable Swift package for implementing the Keys for All licensing system in iOS apps.
Features
- 🔑 Flexible License Levels: Support for Free, Level 1, and Level 2 tiers
- 🎨 Customizable UI: Provide your own colors and styling
- 📦 Feature Gating: Easy-to-use API for checking feature availability
- 💾 Persistent Storage: Built-in UserDefaults storage with protocol for custom implementations
- 🛒 Purchase Integration: Ready for StoreKit integration
- 🎁 Key Sharing: Built-in UI for sharing and donating keys
Installation
Swift Package Manager
Add the package dependency to your Package.swift:
dependencies: [
.package(path: "../path/to/keys-for-all/ios-integration")
]
Or in Xcode:
- File > Add Package Dependencies
- Click "Add Local..."
- Navigate to the
keys-for-all/ios-integrationdirectory
Quick Start
1. Define Your Features
import KeysForAll
enum MyAppFeature: String, CaseIterable {
case premiumThemes
case advancedFilters
case exportHighRes
case batchProcessing
}
class MyAppFeatureProvider: FeatureProvider {
typealias Feature = MyAppFeature
var allFeatures: [MyAppFeature] {
MyAppFeature.allCases
}
func displayName(for feature: MyAppFeature) -> String {
switch feature {
case .premiumThemes: return "Premium Themes"
case .advancedFilters: return "Advanced Filters"
case .exportHighRes: return "High Resolution Export"
case .batchProcessing: return "Batch Processing"
}
}
func category(for feature: MyAppFeature) -> FeatureCategory {
switch feature {
case .premiumThemes: return .visualization
case .advancedFilters: return .advanced
case .exportHighRes: return .core
case .batchProcessing: return .advanced
}
}
func requiredLevel(for feature: MyAppFeature) -> LicenseLevel {
switch feature {
case .premiumThemes, .advancedFilters: return .level1
case .exportHighRes, .batchProcessing: return .level2
}
}
}
2. Set Up Color Provider
struct MyAppColorProvider: ColorProvider {
var primaryColor: Color { .blue }
var secondaryColor: Color { .purple }
var successColor: Color { .green }
}
3. Initialize in Your App
import SwiftUI
import KeysForAll
@main
struct MyApp: App {
@StateObject private var keysForAll = KeysForAllManager(
featureProvider: MyAppFeatureProvider(),
storage: UserDefaultsKeyStorage(suiteName: "group.myapp")
)
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(keysForAll)
}
}
}
4. Check Feature Availability
struct PremiumFeatureView: View {
@EnvironmentObject var keysForAll: KeysForAllManager<MyAppFeatureProvider>
var body: some View {
VStack {
if keysForAll.isFeatureAvailable(.advancedFilters) {
AdvancedFiltersView()
} else {
LockedFeaturePrompt(feature: .advancedFilters)
}
// Or use the built-in gated view
keysForAll.gatedView(for: .batchProcessing) {
BatchProcessingView()
} lockedContent: {
PurchasePromptView()
}
}
}
}
5. Show Keys for All Settings
struct SettingsView: View {
@EnvironmentObject var keysForAll: KeysForAllManager<MyAppFeatureProvider>
@StateObject private var purchaseHandler = MyPurchaseHandler()
var body: some View {
NavigationView {
List {
NavigationLink("Keys for All") {
KeysForAllView(
manager: keysForAll,
colorProvider: MyAppColorProvider(),
purchaseHandler: purchaseHandler
)
}
}
}
}
}
API Reference
KeysForAllManager
The main class for managing licenses and features.
Properties:
keyCount: Current number of keys ownedcurrentLevel: Current license levelunlockedFeatures: Set of unlocked featureslockedFeatures: Set of locked features
Methods:
isFeatureAvailable(_:): Check if a feature is unlockedaddKeys(_:): Add keys to the user's accountremoveKeys(_:): Remove keys (for sharing/donating)gatedView(for:content:lockedContent:): SwiftUI view builder for conditional content
Protocols
FeatureProvider: Define your app's features
allFeatures: Array of all featuresdisplayName(for:): Human-readable namecategory(for:): Feature categoryrequiredLevel(for:): Required license level
ColorProvider: Customize UI colors
primaryColor: Main brand colorsecondaryColor: Secondary brand colorsuccessColor: Success state color
KeyStorage: Implement custom storage
getKeyCount(): Retrieve key countsetKeyCount(_:): Store key countisKeyGatingEnabled(): Check if gating is active
Testing
The package includes a debug mode that can be enabled by setting isKeyGatingEnabled to false, which unlocks all features regardless of key count.
License
This package is part of the Keys for All system. See LICENSE for details.