I didn't know that Settings Bundle exists till two days ago. Anyway, I've tested it with a simple example. As shown in the screenshot below, I have one group, one text field, one slider and two toggle buttons.
I am able to read the values from all of them except the slider. I wonder if it's a bug? I'm using Xcode 14.2. In the following code, the app won't go inside the if clause for the PSSliderSpecifier key.
import UIKit
class ViewController: UIViewController {
// MARK: - Life cyle
override func viewDidLoad() {
super.viewDidLoad()
let defaultValues = [String: AnyObject]()
UserDefaults.standard.register(defaults: defaultValues)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
/* settings */
fetchSettingBundleData()
}
@objc func fetchSettingBundleData() {
let userDefaults = UserDefaults.standard
if let settingsURL = Bundle.main.url(forResource: "Root", withExtension: "plist", subdirectory: "Settings.bundle"), let settings = NSDictionary(contentsOf: settingsURL), let preferences = settings["PreferenceSpecifiers"] as? [NSDictionary] {
var defaultsToRegister = [String: Any]()
for preferenceSpecification in preferences {
if let key = preferenceSpecification["Type"] as? String, let value = preferenceSpecification["Title"] {
defaultsToRegister[key] = value
}
}
userDefaults.register(defaults: defaultsToRegister)
}
if let groupName = userDefaults.string(forKey: "PSGroupSpecifier") {
print("Group name: \(groupName)")
}
if let _ = userDefaults.string(forKey: "PSTextFieldSpecifier") {
if let text = userDefaults.string(forKey: "name_preference") {
print("Textfield \(text)")
}
}
if let _ = userDefaults.string(forKey: "PSToggleSwitchSpecifier") {
if let value = userDefaults.string(forKey: "enabled_preference1") {
print("Toggle \(value)") // 0 or 1
}
}
if let _ = userDefaults.string(forKey: "PSToggleSwitchSpecifier") {
if let value = userDefaults.string(forKey: "enabled_preference2") {
print("Toggle2 \(value)") // 0 or 1
}
}
if let _ = userDefaults.string(forKey: "PSSliderSpecifier") {
print("heck....") // No show
if let value = userDefaults.string(forKey: "slider_preference") {
print("Slider \(value)")
}
}
}
}