Skip to content

alokc83/AgeRangeServiceKit

Repository files navigation

AgeRangeServiceKit

A Swift package for managing and working with age ranges in iOS applications, specifically designed to work with iOS 26.0's new DeclaredAgeRange feature.

🎯 Demo App

This package includes a demo iOS app for testing and development. See AgeRangeServiceKitDemo/README.md for setup instructions.

Important

Apple Developer Account Required: To run the demo app and test the age range service, you must have an Apple Developer account. The age range service capability must be enabled in your app's capabilities section. Without an Apple Developer account, you will not be able to test the age range service capabilities at this time.

Quick Start with Demo App:

  1. Open Xcode and create a new iOS App project in the DemoApp folder
  2. Add this package as a local dependency
  3. Enable the Age Range Service capability in your app's signing & capabilities
  4. Run the app to see AgeRangeServiceKit in action

iOS Swift License

Overview

DeclaredAgeRangeKit helps you implement age-based restrictions in your iOS app using Apple's native age verification system. The package provides:

  • Design-agnostic views - Bring your own UI/design system
  • Simple age checking - Easy-to-use manager with computed properties
  • Environment-based - Uses Apple's @Environment(\.requestAgeRange)
  • No persistence needed - Age data managed by the system

Features

βœ… Age verification using Apple's native API
βœ… Customizable blocking views via @ViewBuilder
βœ… Teen (13-17) checkout restrictions
βœ… Underage (<13) app blocking
βœ… Thread-safe with proper @MainActor handling
βœ… No UserDefaults or custom storage needed

Requirements

  • iOS 26.0+
  • Swift 5.9+
  • Xcode 26.0+
  • Apple Developer account (required for age range service capability)

Warning

The age range service capability is only available to apps with a valid Apple Developer account. You must enable this capability in your app's signing & capabilities to use this package's features.

Installation

Swift Package Manager

Add DeclaredAgeRangeKit to your project:

dependencies: [
    .package(url: "https://github.com/alokc83/AgeRangeServiceKit.git", from: "1.0.0")
]

Or in Xcode:

  1. File β†’ Add Package Dependencies...
  2. Enter the repository URL
  3. Select version and add to your target

Usage

Basic Setup

1. Import the package

import DeclaredAgeRangeKit
import DeclaredAgeRange

2. Check age in your main view

struct ContentView: View {
    @StateObject private var ageManager = AgeRangeManager.shared
    @Environment(\.requestAgeRange) private var requestAgeRange
    
    var body: some View {
        TabView {
            // Your app content
        }
        .task {
            await ageManager.checkAgeRange(using: requestAgeRange)
        }
        .overlay {
            if ageManager.isUnder13 {
                UnderageBlockerView {
                    MyCustomBlockerDesign()
                }
            }
        }
    }
}

3. Block checkout for teens

struct CheckoutView: View {
    @StateObject private var ageManager = AgeRangeManager.shared
    @Environment(\.requestAgeRange) private var requestAgeRange
    @State private var showTeenWarning = false
    
    var body: some View {
        // Checkout UI...
        
        .fullScreenCover(isPresented: $showTeenWarning) {
            TeenCheckoutWarningView {
                MyTeenWarningDesign()
            }
        }
        .onAppear {
            Task {
                await ageManager.checkAgeRange(using: requestAgeRange)
                if ageManager.isTeen {
                    showTeenWarning = true
                }
            }
        }
    }
}

Custom Designs

The package views accept custom content via @ViewBuilder, so you provide the design:

Underage Blocker Example

struct MyUnderageBlocker: View {
    var body: some View {
        ZStack {
            Color(hex: "F5F5F5")
                .ignoresSafeArea()
            
            VStack(alignment: .leading, spacing: 16) {
                Text("Age Requirement Not Met")
                    .font(.system(size: 28, weight: .bold))
                
                Text("Per local laws, users under 13 may not use this app.")
                    .font(.system(size: 17))
                    .foregroundColor(.gray)
            }
            .padding(.horizontal, 40)
        }
    }
}

// Use it:
UnderageBlockerView {
    MyUnderageBlocker()
}

Teen Checkout Warning Example

struct MyTeenWarning: View {
    var body: some View {
        ZStack {
            Color.black.opacity(0.4)
                .ignoresSafeArea()
            
            VStack(spacing: 30) {
                Image(systemName: "exclamationmark.triangle.fill")
                    .font(.system(size: 80))
                    .foregroundColor(.orange)
                
                Text("Checkout Not Available")
                    .font(.title)
                
                Text("Users under 18 cannot complete purchases.")
                    .foregroundColor(.gray)
            }
            .padding()
            .background(Color.white)
            .cornerRadius(16)
        }
    }
}

// Use it:
TeenCheckoutWarningView {
    MyTeenWarning()
}

API Reference

AgeRangeManager

The main manager for age checking:

@available(iOS 26.0, *)
public class AgeRangeManager: ObservableObject {
    public static let shared: AgeRangeManager
    
    // Current age range from system
    @Published public private(set) var currentAgeRange: AgeRangeService.AgeRange?
    
    // Loading state
    @Published public private(set) var isLoading: Bool
    
    // Check age using environment value
    public func checkAgeRange(using requestAgeRange: DeclaredAgeRangeAction) async
    
    // Computed properties
    public var isUnder13: Bool  // User is under 13
    public var isTeen: Bool     // User is 13-17
    public var isAdult: Bool    // User is 18+
}

UnderageBlockerView

View for blocking users under 13:

public struct UnderageBlockerView<Content: View>: View {
    public init(@ViewBuilder content: () -> Content)
}
  • Automatically prevents dismissal (.interactiveDismissDisabled())
  • Accepts custom content for your design

TeenCheckoutWarningView

View for warning teen users at checkout:

public struct TeenCheckoutWarningView<Content: View>: View {
    public init(@ViewBuilder content: () -> Content)
}
  • Automatically prevents dismissal (.interactiveDismissDisabled())
  • Accepts custom content for your design

Examples

Check out the USAGE.md file for detailed examples and patterns.

How It Works

  1. No Storage: Age data is retrieved from the system via @Environment(\.requestAgeRange)
  2. Computed Properties: Age range is checked against your thresholds (13, 18)
  3. Custom UI: You provide all UI via @ViewBuilder closures
  4. Thread-Safe: All @Published updates happen on MainActor

Design Philosophy

Separation of Concerns: The package handles age verification logic, you handle the design.

  • βœ… Package provides the "what" (age checking, blocking logic)
  • βœ… Your app provides the "how" (colors, fonts, layouts)

This allows:

  • Using your design system/tokens
  • Supporting dark mode with your colors
  • Localizing text in your app
  • A/B testing different designs
  • Complete brand control

Testing

// The package gracefully handles unavailable API
// Returns nil for currentAgeRange on older iOS
// Defaults to safe behavior (treats unknown as under 13)

if ageManager.currentAgeRange == nil {
    // API not available or declined
}

Roadmap

  • Add support for custom age gates
  • Localization helpers
  • Example designs library
  • Unit tests
  • CI/CD setup

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built on Apple's Declared Age Range API
  • Inspired by clean architecture principles
  • Design-agnostic approach from component library best practices

Support

Author

Alok Choudhary - @alokc83


Note: This package requires iOS 26.0+ and Xcode 26.0+.

About

wrapper around apple's newest api for AgeRange Checking

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages