I\'m adding a Settings.bundle file to my iOS application. It\'s very minimal with one or two settings. The plist file in the Settings.bundle specifies the defaults, to the S
Based on Andy's answer above, adapted for Swift 4, and avoiding the use of force-unwrap/force-cast:
private func loadDefaults() {
let userDefaults = UserDefaults.standard
let pathStr = Bundle.main.bundlePath
let settingsBundlePath = (pathStr as NSString).appendingPathComponent("Settings.bundle")
let finalPath = (settingsBundlePath as NSString).appendingPathComponent("Root.plist")
let settingsDict = NSDictionary(contentsOfFile: finalPath)
guard let prefSpecifierArray = settingsDict?.object(forKey: "PreferenceSpecifiers") as? [[String: Any]] else {
return
}
var defaults = [String: Any]()
for prefItem in prefSpecifierArray {
guard let key = prefItem["Key"] as? String else {
continue
}
defaults[key] = prefItem["DefaultValue"]
}
userDefaults.register(defaults: defaults)
}