Swift/iOS Integration Guide

Swift Feature Flags

Feature flags for iOS and macOS — remote toggles without App Store updates

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

// Add to Package.swift or use URLSession directly

3. Evaluate flags

import Foundation

class FlagBit {
    static let shared = FlagBit()
    private let sdkKey = ProcessInfo.processInfo.environment["FLAGBIT_SDK_KEY"] ?? ""
    private var cache: [String: Bool] = [:]

    func isEnabled(_ flagKey: String, context: [String: String] = [:]) async -> Bool {
        let cacheKey = "\(flagKey):\(context.hashValue)"
        if let cached = cache[cacheKey] { return cached }

        guard let url = URL(string: "https://flagbit.anethoth.com/api/v1/evaluate") else {
            return false
        }

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue(sdkKey, forHTTPHeaderField: "X-SDK-Key")
        request.httpBody = try? JSONEncoder().encode([
            "flag_key": flagKey,
            "context": context
        ])

        do {
            let (data, _) = try await URLSession.shared.data(for: request)
            let response = try JSONDecoder().decode(FlagResponse.self, from: data)
            cache[cacheKey] = response.value
            return response.value
        } catch {
            return false
        }
    }
}

struct FlagResponse: Decodable { let value: Bool }

// SwiftUI usage
struct ContentView: View {
    @State private var showNewDesign = false

    var body: some View {
        Group {
            if showNewDesign {
                NewDashboardView()
            } else {
                ClassicDashboardView()
            }
        }
        .task {
            showNewDesign = await FlagBit.shared.isEnabled("new-dashboard",
                context: ["user_id": userId, "platform": "ios"])
        }
    }
}

// UIKit usage
class SettingsViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        Task {
            let showBeta = await FlagBit.shared.isEnabled("beta-settings")
            if showBeta {
                addBetaSection()
            }
        }
    }
}

Use Cases for Swift/iOS

App Store Independence

Toggle features without submitting a new build to App Review — changes take effect immediately.

Platform Targeting

Enable features only on iPad, or only on iOS 17+, using context-based targeting rules.

Phased Rollouts

Roll out to 5% of users, monitor crash rates, then gradually increase to 100%.

Kill Switch

Instantly disable a feature causing crashes — no emergency App Store submission needed.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Swift/iOS app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

Cache flag values in UserDefaults. Load cached values first, then refresh from API when online.

Yes — use .task modifier for async flag evaluation as shown above. Bind to @State for reactive updates.

Yes — wrap the async call in a Future publisher for Combine-based architectures.

FlagBit works on any Apple platform with URLSession — watchOS, tvOS, and macOS included.

Start using feature flags in Swift/iOS

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key