Swift Application Configuration (iOS/macOS): Tools and Libraries
Application configuration is a crucial part of iOS and macOS development. It allows you to flexibly manage application behavior without changing code, configure parameters for different environments (dev/staging/prod), and personalize the user experience. In this article, we will look at the main Swift tools and libraries for configuration management.
1. Built-in iOS/macOS Tools
Apple provides several built-in mechanisms for storing and reading configuration data.
1.1 PropertyList (Info.plist)
A classic way to store static configuration. The Info.plist file contains basic application settings: version, bundle identifier, supported orientations, etc.
// Reading a value from Info.plistif let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String { print("Application version: \\(appVersion)")}
// Reading a custom keyif let apiURL = Bundle.main.object(forInfoDictionaryKey: "APIBaseURL") as? String { print("API Base URL: \\(apiURL)")}1.2 UserDefaults
UserDefaults is a simple way to store small amounts of data (strings, numbers, boolean values). It is ideal for user settings.
// SavingUserDefaults.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") ?? "en"1.3 SwiftUI @AppStorage
A modern wrapper over UserDefaults for SwiftUI. It allows you to automatically synchronize state with the UI.
struct SettingsView: View { @AppStorage("isDarkMode") private var isDarkMode = false @AppStorage("username") private var username = "Guest" var body: some View { Form { Toggle("Dark Theme", isOn: $isDarkMode) TextField("Username", text: $username) } }}2. Libraries for Advanced Configuration
2.1 Firebase Remote Config
Cloud configuration from Google. Allows you to change application parameters without publishing an update to the App Store.
import FirebaseRemoteConfig
class RemoteConfigManager { static let shared = RemoteConfigManager() private let remoteConfig = RemoteConfig.remoteConfig() func setup() { let defaults: [String: NSObject] = [ "welcome_message": "Welcome!" as NSObject, "max_items_count": 20 as NSObject ] remoteConfig.setDefaults(defaults) remoteConfig.fetch(withExpirationDuration: 3600) { status, error in if status == .success { self.remoteConfig.activate() } } } var welcomeMessage: String { remoteConfig.configValue(forKey: "welcome_message").stringValue ?? "" } var maxItemsCount: Int { Int(remoteConfig.configValue(forKey: "max_items_count").numberValue ?? 20) }}2.2 SwiftyUserDefaults
A convenient wrapper over UserDefaults with Codable support and type-safe keys.
import SwiftyUserDefaults
extension DefaultsKeys { static let isFirstLaunch = DefaultsKey<Bool>("isFirstLaunch", defaultValue: true) static let userProfile = DefaultsKey<Data?>("userProfile")}
struct UserProfile: Codable { let name: String let age: Int}
// UsageDefaults[.isFirstLaunch] = false
let profile = UserProfile(name: "Ivan", age: 30)if let data = try? JSONEncoder().encode(profile) { Defaults[.userProfile] = data}
if let profileData = Defaults[.userProfile], let decoded = try? JSO