Ios development-application settings and user default settings [1. Use of bundle] And iosbundle
On the iphone, the app will have an exclusive app setting in "Settings". By selecting this menu interface, you can enter and change various options in it, helps users easily set their personal preferences and habits.
In this section, we hope to discuss the development and call of application settings in the user default mechanism through the introduction of bundle and the application of plist files.
[Development Environment: Xcode: 7.2 iOS Simulator: iphone6S plus By: ah Left]
1. Set bundle introduction and interface display
A bundle is a set of files that come with an application. You can enter and change the preference settings of an application. For example, in figure 1, the development name is at the bottom:"Application settings"App.
[Figure 1 setting the application interface]
Set the app to act as the general User interface of the ios User default mechanism. User default settings are part of the system for saving and obtaining preference settings. In ios apps, users' default settings are implemented by the NSUserDefaults class (similarly, in mac, this class is also used to save and read preference settings ).
NSUserDefaults and NSDictinary?
The application uses NSUserDefaults to read and save the preference data in key-value pairs, just like retrieving data from the NSDictionary object through the key,The difference is that the data in NSUserDefaults will be permanently saved to the file system.Instead of storing it in an object instance in the memory.
This section adds and configures a bundle package by developing an application. Then, access and edit the preference settings from the application, and edit the system preference settings to change the app control data.
In this process, data is synchronized and updated.
The configuration page [Figure 2] is as follows:
[Figure 2 application settings page] (the app name is "application settings ".. Here, More is the subview of the last option)
The app page [Figure 3] is as follows:
Figure 3 app Interface]
So, start creating a project ~~
2. Create a boundle package
1. First passCreate a project named "application settings". Remember to select "iphone" or "universal" as the Devices and deselect "Use CoreData.
2. Create a bundle:
Click the "application settings" folder, select File> New> File from the upper left corner, select Resource in the ios section, and select the set Bundle icon (see figure 4)Click Next, keep the default Settings for the name, and click Create.
Figure 4 Create a bundle package]
At this point, we can see such a "Setting. bundle "bundle, expand. bundle settings, You can see "en. the Iproj folder is mainly used for localized applications. This article does not discuss the content of localized applications. It mainly introduces Root. plist.
3. Set the attribute list
Click Root. plist to view the editor window. You can see the Xcode attribute list Editor (see figure 5 ),
[Figure 5 property list above Key: Key Type: Type Value: Value]
If the editing window looks different from the image, right-click the blank area and select:"Show Raw Keys/Values ".For example:
[Note: The image in the property list plist file discussed in this article is"Show Raw Keys/Values"]
Figure 6Show Raw Keys/Values]
View the organizational structures in plist. Attribute lists are essentially dictionaries. The types and values of each stored item must be retrieved by a specific key.NSDictionaryBoolean, Data, Date, Number, StringData can be saved. In additionDictionaryIt can also be used as a node for saving the dictionary, andArrayNodes can also store an ordered list containing other nodes. WhereDictionary andArrayIt is a unique attribute list node type that can contain other nodes..
(Note:NSDictionary can use most objects as keys, but the middle keys of the Dictionary node in the attribute list must be strings, but any node type can be selected as the key value .)
The following describes how to set and edit items in Root. plist.
Here we can see the StringsTable entry, which is used to convert an application to another language. This article will not use it, but it can also be kept, although it is not very useful ..
BesidesStringsTable, the attribute list also has a node named "PreferenceSpecifiers", which is an array, which stores a groupDictionary node, eachA Dictionary node represents a preference setting item that the user can modify or a subview of the setting page that the user can access.
You will notice that there are four Item nodes in this attribute list template, which have no actual effect in this application. Therefore, click Item1 to 3, delete them by delet, and leave only Item0.
(Tip: To select an item in the attribute list, it is best to click one end or the other end of the Key column. Otherwise, the drop-down menu of the Key column is easy to open .)
Click the triangle on the left of Item0 to show two rows of data.The Type key value PSGroupSpecifier indicates that this entry is the beginning of a new group, and each entry following it will be part of this group until a new Type key value isPSGroupSpecifier starts another group.Therefore, Item0 in PSGroupSpecifier must always bePSGroupSpecifier type. This ensures that at least one group exists in each configuration list.
Title key in Item0, Use and set the title in this group. We can see later in system settings.
In Item0 (Group-Group), the first Group represents the value of the Type item, and the second Group represents the value of the Titile item. This is a convenient way for Xcode to help you observe the content of the bundle.
We changed the Title in Item0 from Group to Group Info.
Next we will add two text boxes in settings that allow users to enter their accounts and passwords.
First, click the expanded triangle on the left of Item0 to close it. Then, select Item0 and press return.
A new row of the same level as Item0 is added. A drop-down menu [Figure 7] is displayed, showing the default TextField. SelectTextField to make the menu disappear.
Figure 7]
Expand Item1, as shown in figure 8.TitleThe key value is changedZhanghao(Used to display in settings ),KeyThe key value is changedZH(As the key for getting and editing this text box, it worksDictionarySimilar)
Figure 8]
In the last line of the Item, that is, the Key line, press return to add a new line, set its Key to AutocapitalizationType, and click the rightmost of the line, you can also enter Words in the text box.
Similarly, add AutocorrectionTypeIf No is selected, the value entered in the text box is not automatically corrected..
(Remember that the title is displayed on the set screen; the key is used to store the value ;)
Figure 9]
Next, add the Password text box. You can add Item2 entries by adding Item1. The data of each row is shown in figure 10, where IsSecure is Boolean, this text box is a password box that hides user input text. (Of course, to create Item2, you can also click Item1, press Command + C, and then press Command + V to copy the same new Item2 entry, and then expand Item2 to modify the value, add moreIsSecure.)
Figure 10]
4. Add the application icon and run it to view the changes in system settings.
Click Assets. xcassets in the folder. Click the AppIcon item in it, as shown in Figure 11 below. You can see the settings of the three types of icons from left to right.
The first one is used to display your app on the iphone's home screen; the second one will appear when you use Spolight to search for apps on the iphone; and the third one will be displayed in the "Settings" app;
If you need to process the image yourself, you can click the box to be set, 11, Siz is 60x60, but it requires 2x, so the image size is 120x120.
(You can use the image browser that comes with mac to process the image. Double-click to open the image to be processed, and click "tool"> "resize" at the top of the screen. When you enter 120x120, remember that the unit you selected isPixels!)
Figure 11]
In this case, you only need to set the"Set. Run the project by Command + R ~~~
At this time, the simulator opened a blank project, not in a hurry. Click the simulator and then pressCommand + Shift + HReturn to the home page of the simulator, and find the same application settings as those in the mobile phone. At this time, we pull down to the bottom, and we can see that this line is added. Click, as shown in, enter the account and password in the text box:
Figure 12]
This is how the bundle package is configured.
Next, we will add more items.
Item3:Multi-Value Field.
Fold Item2 and click. Press return to add Item3. In the pop-up menu of the Key column, selectMultiValueClick the triangle on the left to expand Item3. Use the method of editing the list to add the following six lines of entries so that the entries are consistent.
Figure 13]
Where,TtilesArray, used to save a set of values that can be selected by the user;
ValuesArray, used to save a group of values stored in the user's default settings;
Ttiles and ValuesTherefore, if you select the first item, setValuesThe first value of the array, but what you see isTitles. ThereforeTtiles/ValuesIt provides easy-to-understand options for users. When Item3 is created,Default Value"Behavior automatically added by Xcode, representing the default value. Set the value to the first Ensign.
This is the creation of multi-value fields.
Item4:Switch Settings
Close Item3 and press the return key to create Item4. In the pop-up menu, select "Toggle Switch" and set it to the following content:
Figure 14]
Next, add the slider:
Use the preceding methods to create Item5 and Item6, respectively, as shown in Figure 15]
Item5 indicates setting another group with the title "Warp Factor ".
In Item6, the lower face value represents,
Minimum Value: Minimum Value; Maximum Value: Maximum Value;
Min Value Image Filename: the leftmost icon of the slider; Max Value Image Filename: the rightmost icon of the slider;
(Note: The tag_remove_24 and tag_add_24 icons of the slider cannot only be placed in the main folder, but must be placed in the Settings bundle to use the Min/Max Value Image Filename setting .)
[Figure 15 settings of Item5 and Item6]
In this case, run the command to check the effect.
[Figure 16 Add Item4, 5, and 6]
Sometimes, we need to add a subview to the master setting view. For example, if you want the slider to be different from the account and password settings, you can go to another sub-view.
We can add two new items to the bundle as follows:
[Figure 17] (Item7 indicates adding a new group. You can click Copy Item0 and paste it to add the new group Item7 and Item8 table subviews, the More parameter corresponding to Filename indicates that the sub-view is. plist "load .)
Next, we need to add a new property file named "More. plist". You can add it to the main file first.
[Figure 18 Add a plist file]
Click the Preference Items item in "Root. plist ".And then copy to the More. plist attribute list to delete items except Item5 and Itme6,
[Figure 19 edit the More. plist attribute list]
Note that Xcode does not allow you to directly add new files to the set bundle. Therefore, you must right-click "Setting. bundle "select" show in Finder "to open the window, and then click More. drag the plist file.
Now, we will complete the basic operations for setting the bundle. The effect is shown in Figure 20. We can compile and run the system settings of the test project application. You can also change other items in the property list by yourself.
In the next section, we will explore how to read the settings in the application.
Figure 20]
By: Ah, left ~