iOS, in addition to the application, provides a centralized place to modify user preferences and serialize to a file.
Create a project
1.utility Application Template
A Storyboard
A main view Mainviewcontroller
An auxiliary setting Flipviewcontroller
Using the provisioning bundle
When the setting application starts, it examines the settings bundle for each application and adds a settings group for each application that contains the provisioning bundle.
To add a provisioning bundle to a project
1. New file, resource, setting bundles
2. Expand Settings.bundles, set the property list file Root.plist
Points:
1) in Nsdictionary, you can use most objects as keys,
However, in the attribute list dictionary node, the key must be a string type, but the value can be any other property list type.
Property list types are: Nsdictionary, Nsarray, NSData, NSDate, Nsboolen, NSString, NSNumber
(control+ right-click the Show row keys/values option to switch to show real key-value names)
2) key is @ "Preferencespecifiers", value is array
Array, which also stores a series of dictionary, each corresponding to an entry for the set
Type can be: Group (group), Multi value (multi-value selection), Slider (slider), text Field (text input), Title, Toggle switch (switch)
3) All entries from one grouping to another are grouped in this group.
4) If you want to use the icon in the application bundle, you need to copy the PNG into the bundle.
Because the provisioning bundle is part of the settings application, the settings application cannot access the sandbox of the app.
5) Use of the attribute List Editor Note:
If a row is selected, return
1. If expanded, the child node will be inserted in the first position
2. If not expanded, a sibling node is created later
3. Support Copy and paste
6) Add Application icon
AppSetting Target-"Summary-" Find the configuration section of the application icon and drag it into the PNG file.
7) Add a child view
Type: Pschildpanespecfier
Link to a different plist file
Create More.plist methods, copy root.plist, rename, modify
You can no longer add new files to the application bundle in Xcode, and the save for the properties file editor does not support saving new files to the settings bundle
So, the only way to do this is to create a new file, save it somewhere, and then drag it into the provisioning bundle through the finder.
By editing the Root.plist file in a certain format, you can implement the established editing interface in the settings application.
Read settings in the application
To modify the default settings in the application
The Nsuserdefaults class provides a very convenient way to get common types, such as Floats,doubles,intergers,booleans,urls.
So a Nsuserdefaults object must be an attribute table, which means we can store nsdata,nsstring,nsnumber,nsdate,nsarray,nsdictionary these instances.
If you want to store other types of objects, you need to archive them and create a nsdata to implement the storage.
Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
When storing, other than the NSNumber type uses the corresponding type, the other is using Setobject:forkey:
[Userdefaults setinteger:myinteger forkey:@ "Myinteger"];
[Userdefaults setfloat:myfloat forkey:@ "Myfloat"];
[Userdefaults setdouble:mydouble forkey:@ "MyDouble"];
[Userdefaults setobject:mystring forkey:@ "myString"];
Nsinteger Myinteger = [userdefaultes integerforkey:@ "Myinteger"]; float myfloat = [userdefaultes floatforkey:@ "Myfloat"]; Double MyDouble = [Userdefaultes doubleforkey:@ "MyDouble"]; NSString *mystring = [userdefaultes stringforkey:@ "myString"]; NSDate *mydate = [userdefaultes valueforkey:@ "MyDate"]; Nsarray *myarray = [userdefaultes arrayforkey:@ "MyArray"]; Nsdictionary *mydictionary = [userdefaultes dictionaryforkey:@ "MyDictionary"];
You may ask a question:
Where does the NSUSERDEFAUTLS store the data??? I have not shown the specified path???
Data stored with Nsuserdefaults still exists the next time the program runs, where does it store the data? How can I clear?
In fact it is stored in a plist file built into the application, which can be seen according to the path. For example, this is your program sandbox location/userslibrary/application support/iphonesimulator/4.1/applicati*****/ 29788E40-AF47-45A0-8E92-3AC0F501B7F4/, (this is where the application corresponds on the Mac)
There is a/library/prefereces, there is a plist file, stored is your userdefaults
If you want to erase it, use Removeobjectforkey or delete the sandbox, which is your application and then reinstall it.
Note that when the settings are modified, be careful to synchronize the storage in time
[Defaults Synchronize] This method will force saving as a saved modification and will synchronize unmodified settings to defaults
Registering default values
Start the app for the first time, set a default value of 1. If not set, no value is read from Nsuserdefault if the values have not been modified. The 2.registerDefaults method will determine if there is a keyvalue, and if so, the user has set it up and no longer modifies it. -(void) applicationdidfinishlaunching: (nsnotification *) anotification{nsdictionary *defaults = @{ Kwarpdrivekey: @YES, Kwarpfactorkey: @5, Kfavoritesinkey: @ "greed"}; [[Nsuserdefaults Standarduserdefaults] registerdefaults:defaults]; return YES;} Nsdictionary Shortcut Creation Method @{key:value, Key:value,......} Quick way to create Nsarray: @[obj, obj, ...] Quick way to create NSNumber: @5@yes@2.1
Make settings Live effectively
Apply from background to wake up, override get, refresh user settings
notification, Notification Center
Uiapplicationwillenterforegroundnotification
The Notification center mechanism, notification subscriptions and cancellations, responses, need to be examined in detail in a separate topic.
Summarize:
1. Focus on creating and editing root.plist files in the "set bundle"
Command+n-resource-"settings bundle
2. By editing the "set bundle", the settings application can read the plist file and generate an established user interface
3. In your app, read and set user settings, use nsuserdefaults this singleton class
[Nsuserdefaults Standarduserdefaults]
Objectforkey:
Setobject:forkey:
After the changes, remember to synchronize the changes to the local storage in time
Synchronize
4. Remember to register the default settings in the app delegate, using the method
Registerdefaults
12th Lesson: Application Settings and User Defaults