Swift Application Configuration (iOS/macOS): Best Practices and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Swift Application Configuration (iOS/macOS): Overview of Tools and Libraries



Application configuration is one of the key aspects of iOS and macOS development. The flexibility, testability, and security of your product depend on proper settings management. In this article, we will review standard configuration mechanisms in Swift, as well as popular libraries and tools that simplify managing application parameters.



Built-in Configuration Mechanisms in Swift



1. Info.plist and Xcconfig

The basic way to store application metadata is the Info.plist file. It contains the bundle version, identifier, application name, and other system parameters. For more flexible configuration management (e.g., different values for Debug/Release), .xcconfig files are used.



// Example.xcconfigAPP_NAME = MyAppAPI_BASE_URL = https://api.example.comBUNDLE_ID_SUFFIX = .dev


In Swift, these values can be retrieved via Bundle.main:



func getConfigValue(key: String) -> String? {    guard let value = Bundle.main.object(forInfoDictionaryKey: key) as? String else {        return nil    }    return value}

let baseURL = getConfigValue(key: "API_BASE_URL")


2. UserDefaults

For storing user settings (theme, language, font size), UserDefaults is used. This is a simple and fast way to save small amounts of data.



// SavingUserDefaults.standard.set(true, forKey: "isDarkMode")UserDefaults.standard.set("ru", forKey: "language")

// Readinglet isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode")let language = UserDefaults.standard.string(forKey: "language") ?? "en"


3. SwiftUI AppStorage

SwiftUI has a built-in property wrapper @AppStorage that automatically synchronizes with UserDefaults:



struct SettingsView: View {    @AppStorage("isDarkMode") private var isDarkMode = false    @AppStorage("fontSize") private var fontSize = 14.0

var body: some View { Form { Toggle("Dark Theme", isOn: $isDarkMode) Slider(value: $fontSize, in: 10...30) { Text("Font Size") } } }}


Third-Party Libraries for Configuration



Although standard tools cover basic scenarios, real projects often require more advanced solutions. Let's look at several popular libraries.



1. SwiftyUserDefaults

A wrapper around UserDefaults with typed keys and Codable support. It helps avoid errors associated with string keys.



  • Support for all standard types and Codable
  • Code generation via Sourcery (optional)
  • Simple and concise syntax


import SwiftyUserDefaults

extension DefaultsKeys { var isDarkMode: DefaultsKey<Bool> { .init("isDarkMode", defaultValue: false) } var userName: DefaultsKey<String?> { .init("userName") }}

// UsageDefaults[\\.isDarkMode] = truelet name = Defaults[\\.userName] ?? "Guest"


2. PropertyKit

A library for declarative configuration description. It allows grouping settings and managing their visibility.



  • Support for sections and nested groups
  • On-the-fly value validation
  • Integration with Combine for reactive UI updates


import PropertyKit

struct AppConfig: PropertyGroup { @Property(key: "api_url", default: "https://api.example.com") var apiURL: String

@Property(key: "max_retries", default: 3, validator: { $0 > 0 && $0 < 10 }) var maxRetries: Int

@Property(key: "feature_x_enabled", default: false) var featureXEnabled: Bool

Recommendations