Swift Application Configuration (iOS/macOS): Tools and Libraries
Configuration of mobile and desktop applications in Swift is a key development stage that determines the flexibility, security, and maintainability of the code. In this article, we will review the main approaches, libraries, and tools for configuring applications for iOS and macOS that will help you manage parameters at runtime, store sensitive data, and adapt behavior for different environments.
1. Built-in Configuration Mechanisms in Swift
Before turning to third-party libraries, it's worth mastering the native tools provided by Apple.
1.1 UserDefaults — Simple Settings Storage
UserDefaults is the most common way to store small amounts of data: user preferences, flags, strings, numbers. Usage example:
import Foundation
// Saving a valueUserDefaults.standard.set(true, forKey: "isDarkModeEnabled")UserDefaults.standard.set("ru", forKey: "appLanguage")
// Readinglet isDarkMode = UserDefaults.standard.bool(forKey: "isDarkModeEnabled")let language = UserDefaults.standard.string(forKey: "appLanguage")Important: do not use UserDefaults for storing passwords, tokens, or large amounts of data — use Keychain and Core Data for that.
1.2 Property Wrappers — Modern Swift Approach
Starting with Swift 5.1, you can use @AppStorage (for SwiftUI) and custom Property Wrappers to encapsulate configuration logic:
import SwiftUI
struct SettingsView: View { @AppStorage("isDarkModeEnabled") var isDarkMode: Bool = false @AppStorage("appLanguage") var language: String = "en"
var body: some View { Toggle("Dark Theme", isOn: $isDarkMode) Picker("Language", selection: $language) { Text("Russian").tag("ru") Text("English").tag("en") } }}This code automatically synchronizes with UserDefaults and updates the UI.
1.3 plist Files and Info.plist
.plist (Property List) is a configuration storage format used for static data: app version, bundle identifier, permissions. You can also create custom plist files for storing environment configurations (dev/stage/prod):
// Loading from a custom plistif let path = Bundle.main.path(forResource: "Config", ofType: "plist"), let config = NSDictionary(contentsOfFile: path) as? [String: Any] { let apiURL = config["apiBaseURL"] as? String ?? "" print(apiURL)}2. Libraries for Advanced Configuration
2.1 XCConfig — Managing Build Schemes
.xcconfig files are a standard Xcode tool for setting environment variables. They allow you to switch API endpoints, keys, and flags without changing code:
// Config.xcconfigAPI_BASE_URL = https://api.dev.example.comAPI_KEY = dev_key_123In code, use Bundle.main.infoDictionary or a custom manager:
struct AppConfig { static let apiBaseURL: String = { Bundle.main.infoDictionary?["API_BASE_URL"] as? String ?? "" }()}2.2 SwiftUI Environment — Passing Configuration Through the Environment
In SwiftUI, you can inject configuration objects via @EnvironmentObject or EnvironmentValues. This is convenient for global settings (e.g., theme, language, timeouts):
class AppConfiguration: ObservableObject { @Published var isPremium: Bool = false @Published var apiVersion: String = "v2"}
struct ContentView: View { @EnvironmentObject var config: AppConfiguration
var body: some View { Text("API Version: \\(config.apiVersion)") }}< Also in library
Kotlin Standard Library: A Complete Overview
Working with S3 and Cloud Storage in JavaScript (Node.js) — @aws-sdk/client-s3 Library
HTML Parsing in Swift (iOS/macOS): SwiftSoup Library
Caching in Kotlin: Redis, Caffeine and Ehcache — A Complete Guide