ios– Add Settings Bundle application Preferences to Settings application

Source: Internet
Author: User
Tags switches

The iphone allows applications to add custom preferences to settings applications, and developers can specify these settings programmatically, but the way they are added to Settings applications provides a friendly GUI. Custom preferences support a variety of data interaction styles, including text, passwords, switches, sliders, and level two selection pages. However, it is important to avoid sensitive information here.

IOS There are three main ways that applications store information:

1. The Singleton class Nsuserdefaults:nsuserdefaults class works like Nsdictionary, where all preferences are stored in the Nsuserdefaults singleton as key/value pairs.

2. Set Bundle (Settings bundle): Provides an interface for configuring an application through an iOS application Settings.

3. Direct access to the file system: Ability to read files belonging to the iOS file system portion of the current application. Common, such as database files.

Setting up a bundle is also an edit of a plist file, which has the advantage of being able to manipulate it through the Xcode plist editor, without having to write extra code, simply defining the data to be stored and its keys in the editor.

to create the Settings preference menu, follow these steps:

1.New file ..., select Resource, select Settings Bundle, create a settings.bundle, default name. It is recommended to add to the supporting files group.

2. Open the Settings.bundle entry in the Project window, you should see the root.plist and en.lproj files, where en.lproj is about setting up the bundle internationalization file, temporarily not considered. Open the Root.plist file, the file is essentially a dictionary, is stored and manipulated by key-value pairs.

The types of nodes in the 3.root.plist file are: Boolean, Data, Date, Number, String (basic type), dictionary and array (the type that can contain other nodes).

4. Set the attribute list in root.plist to follow a specific format. Where we can operate in the preference items array (type array), delete or modify the default entry inside this item, if any. Under normal circumstances should love be a grouping of four subkeys, input boxes, switches, sliders (my Xcode version is 6.3.2). Note, however, that the (direct) subkey of the Preference items can only be of type dictionary.

Setting the file Root.plist in the bundle determines how the application preferences are rendered in the application settings. There are 7 types of preferences, namely:

Text field: TextBox. The key is Pstextfieldspecifier, an editable text string. Often used to save user-defined information, such as nicknames.

Title: Caption. The key is Pstitlevaluespecifier, a read-only text string. Often used to display unchanging information, such as version information.

Toggle switch: Switch. Key is pstoggleswitchspecifier, switch button.

Slider: Slider. The key is Pssliderspecifier, and the value is in a specific range of sliders.

Multi Value: Multi-value. The key is Psmultivaluespecifier, drop-down list.

Group: Group. The key is Psgroupspecifier, and the title of the preference logical group. Often used to set the grouping of items.

Child Pane: Sub-pane. The key is Pschildpanespecifier, child preference page. Often used for selection menus with more options.

Tips:

In the current Xcode version, you can quickly create individual menu items. Clicking the key column of a menu item pops up a menu of shortcut creation items. Select the type you want to automatically create some key-value pairs, and then modify them slightly.

here's how to use the various menus:

1.Text Field : Text box. The following key options are available for the text box:

Autocapitalization Style: Automatic capitalization. There are four values: none (None), sentences (the first letter of the sentence), Words (capitalize the first letter of the word), all characters (capitalization of all letters).

AutoCorrection Style: Automatically correct spelling, if open, you enter a nonexistent word, the system will draw the red line hint. There are three values: default, no AutoCorrection (not automatically corrected), AutoCorrection (auto correct).

Default value: Defaults. Note that the default value, which includes the other menus, is just the defaults, and once the user changes directly or indirectly, the default value no longer works, and all that is displayed or obtained after this is the user's most recent set value.

Identifier: Sets the key that is used to get the value of the set item in the program.

Keyboard Type: Keyboard style. There are five values: Alphabet (alphabet, default), Numbers and punctuation (numbers and punctuation), number Pad (digital panel), URL (more than Alphabet. com and other domain name suffixes), Email Address ( More than Alphabet "@" symbol).

Text Field is secure: whether it is a secure text (password). If set to Yes, the content appears as a dot symbol.

Title: Hint text. Used to prompt the contents of the text box before your text box.

Type: Set text box, this can only be selected text Field.

After the setup is complete, your text box entry might be something like this:

2.Title : Title. There are several key options for the title:

Default value: Defaults. This will be displayed to the right of the set item, gray font.

Identifier, title ibid.

Titles and values: for specifying the optional value of default value, such as setting 5 values respectively under titles and values, you can specify that default value is a value in values, The corresponding entry in the titles will be displayed in the settings. In titles and values, only the corresponding title is displayed, and only the corresponding value is fetched. Note that when you modify the value of this title by code, you want to confirm that the values are included. Note that the titles and values here do not generate a child settings page in the Settings page for selecting value.

Type: Set the title, this can only choose title.

After the setup is complete, your title entry might be something like this:

3.Toggle Switch : Switch button. There are several key options available for switching items:

Default value: Defaults.

Identifier, title ibid.

Type: Set the switch, this can only be selected toggle switch.

Value for on and value for OFF: Sets the values for the switch state.

After the setup is complete, your switch entry might be something like this:

4.Slider : Slider. There are several optional keys for the slider entry:

Default Value, identifier ibid.

Minimum value, Maximum value: Minimum and maximum.

Min value image filename, Max value image filename: The picture displayed around the slider bar. The image size must be 21*21 and placed in the Settings.bundle package (the contents of the package are displayed in the Finder and then pasted).

Type: Set the slider, this can only select slider.

After the setup is complete, your slider entry might be something like this:

5.Multi Value : Multi-value. There are several key options for multivalued entries:

Default value: Defaults.

Identifier, title ibid.

Type: Set the switch, this can only choose multi Value.

Titles and values: the same as above, but this multi-valued titles and values will generate a child settings page in the Settings page for selecting value.

6.Group : Grouping.

If you want to add groups to your preferences, then use Group: Create a Preference Items subkey (such as "Item 0"), type "Dictionary", and contain two types as "String" with the name "type", respectively: and "Title", the values are: "Group" and "GroupName" (GroupName is your custom group name, can be any name). Note that the grouping ends until the next sibling is encountered, or when the last item of the sibling is encountered.

After the setup is complete, your FE entry might be like this:

7.Child Pane : Child pane. This can define a level two menu, with an optional key of three:

Type: Create sub-pane required child Pane.

Title: Title, the caption that is displayed on the parent page in the Application Preferences menu.

Filename: Specifies the plist file corresponding to the Child pane menu, similar to the default root.plist. Do not include a suffix when completing this entry. In addition, this file is to be put together with the Root.plist file. Right-click the Root.plist file and select Show in Finder. Then right-click the root.plist file in the Finder window to select Copy. The default copy is copied to the next root.plist in the current directory. Then modify a meaningful name (change it to child.plist). Then edit the Child.plist file, right-child.plist the root project, select the property List type in the iphone Settings plist. When you edit this file again, you will be prompted automatically.

Child.plist file stringstable This item is optional, it is recommended to retain, and modify the key to Strings filename (and root.plist in the same), the value of its file name, here is the child, do not bring the file suffix.

Design settings menu items that's all that much. These setting data will be stored in the Nsuserdefaults standarddefaults, each setting corresponding to the identifier is key. Then just press the nsuserdefaults operation to set the item.

Here's a piece of code to get the Setup item:

1- (void) Readingsettingspreference2 {3     //Get Settings.bundle Path4NSString *settingsbundle = [[NSBundle mainbundle] Pathforresource:@"Settings"OfType:@"Bundle"];5 6     if(!settingsbundle)7     {8NSLog (@"can't find the Settings.bundle file");9         return;Ten     } One  A     //read the configuration information inside the Settings.bundle -Nsdictionary *settings = [nsdictionary dictionarywithcontentsoffile:[settingsbundle stringByAppendingPathComponent:@"root.plist"]]; -Nsarray *preferences = [Settings Objectforkey:@"preferencespecifiers"]; theNsmutabledictionary *defaultstoregister =[[Nsmutabledictionary alloc] initwithcapacity:[preferences Count]]; -  -      for(Nsdictionary *prefspecificationinchpreferences) -     { +NSString *key = [prefspecification objectforkey:@"Key"]; -         if(Key) +         { A[Defaultstoregister setobject:[prefspecification Objectforkey:@"DefaultValue"] Forkey:key]; at         } -     } -  - [[Nsuserdefaults standarduserdefaults] registerdefaults:defaultstoregister]; -  - [[Nsuserdefaults standarduserdefaults] synchronize]; in  -     //TODO: Reading the specified data to  +}

ios– Add Settings Bundle application Preferences to Settings application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.