You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repo shows the different ways to implement a Settings Bundle
Things to watch out for:
Do not rename the Settings Bundle (if you need to use it for multiple schemes make seperate folders and then add those Settings Bundles to the folders to keep them seperate)
**** (I made this mistake) RegisterDefaults initializes values for the UserDefaults in the current process. Meaning it is not persistent. If you want persistent data use UserDefaults.standard.setValue(Any?, forKey: String?) OR set(value: Any?, forkey: String?)
Make sure that each Settings Bundle is targeting the correct Targets, if you have multiple targets (Dev, QA, Prod)
Make sure that before putting custom settings that the top level contains a group
Double check the names for the keys in the settings Bundle
Make sure to synchronize defaults if the app exits suddenly in the app delegate
Make sure to set the identifier values approrpriately as this is what you will be using to grab the values later on.
When adjusting the types and groups there are two ways of moving them around. One way is to copy and paste items as necessary, the other is to use source code on the info plist and start configuring the plist that way. *If you do end up using source code make sure that elements are placed properly.
Steps to configure your Settings Bundle:
Add files - Under Resources choose Settings Bundle
Make a folder in your Project Directory and change the location of your Settings Bundle
You can repeat these steps for multiple schemes
Open up Root.plist under your Settings Bundle
Under Preference Items is where you start building your settings
Always start out with a Group then the settings type underneath
How to register your settings Bundle in code:
Get Settings from Bundle
Get the Root.plist file as a dictionary
Get the PreferenceSpecifiers as an array of dictionaries
Set a dictionary to all the default values and their respective keys in a for loop.
Register userdefaults with the above dictionary
func registerSettingsBundle(){guardlet settingsBundle =Bundle.main.url(forResource:"Settings", withExtension:"bundle")else{NSLog("Could not find Settings.bundle")return;
}guardlet settings =NSDictionary(contentsOf: settingsBundle.appendingPathComponent("Root.plist"))else{NSLog("Could not find Root.plist in settings bundle")return}guardlet preferences = settings.object(forKey:"PreferenceSpecifiers")as?[[String:AnyObject]]else{NSLog("Root.plist has invalid format")return}vardefaultsToRegister=[String: AnyObject]()forvar pin preferences {iflet k =p["Key"]as?String,let v =p["DefaultValue"]{NSLog("%@","registering \(v) for key \(k)")defaultsToRegister[k]= v
}}Userdefaults.standard.register(defaults: defaultsToRegister)}
Grabbing specific values from the settings:
Grabbing values is easy, simply use Userdefaults and the identifier key that you set earlier for each type.
Note that I am using Userdefaults.standard.string(forKey: String, you can use whatever is apropriate for your case. i.e. bool(forKey: String).