IOS设置项相关----Preferences and Settings Programming Guide

来源:互联网 发布:mac好还是windows好 编辑:程序博客网 时间:2024/05/21 23:40

1.    概述

大部分APP设置项都通过Cocoa preferences system:userdefaults system完成。

 

2.    关于user defaults system

2.1  创建一个正确的preference

使用简单的数据值、数据类型

支持string、number、date,也支持NSData,不推荐使用。

2.2  提供一个设置界面

经常变化的设置项使用APP中的自定义UI,不常变化的设置项通过SettingsBundle实现。

自定义设置项UI,没有标准的实现方案,按自己的需要实现。

2.3  Preferences的组织结构

每个设置项由三部分构成:

²  它所在在的域,比如APP专有设置项、系统设置项。

²  它的名字,是一个NSString对象。

²  它的值, (NSData, NSString, NSNumber, NSDate, NSArray,or NSDictionary)

搜索设置项时,在NSUserDefaults对象的搜索列表中进行,如下表:(按顺序搜索)

Domain

State

NSArgumentDomain

volatile

Application (Identified by the app’s identifier)

persistent

NSGlobalDomain

persistent

Languages (Identified by the language names)

volatile

NSRegistrationDomain

volatile

 

TheArgument Domain

它由命令行参数组成(如果程序为命令行启动),使用 NSArgumentDomain 常量标识,系统自动把命令行参数放到这个域中。

 

TheApplication Domain

包含APP特定的设置项,存储在当前用户UserDefaults数据库里。 因为这个域是对特定app的,所以域的内容是和appbundle标识绑定的。它的数据文件名为<ApplicationBundleIdentifer>.plist, 这里的<ApplicationBundleIdentifer> 指 app的 bundle标识。

 

TheGlobal Domain

包含对所有APP有效的设置项,通过NSGlobalDomain 常量标识。这个域是系统Framework用来存储整个系统适用的设置项值,不应该被APP来存储特定APP的值。如果你想修改GlobalDomain中的设置项,那么应该在applicationDomain中加入同名的设置项,来设置值。

 

TheLanguages Domains

对于AppleLanguages 设置项中的每一种语言,系统把语言相关的(language-specific)设置项值存入到基于这个语言名字命名的特定域中。很多Foundation中的类(比如:NSDate、NSDateFormatter等等)使用特定语言域中的信息修改它们的行为。

 

TheRegistration Domain

为设置项提供默认值,如果设置项没有在其它域中明确设置过。

 

2.4  获取preference值

通过使用NSUserDefaults来获取APP的设置项,每个APP为它提供一个单例的实例,通过standardUserDefaults方法获取,可以用它:

²  在APP启动时,为APP设置项指定默认值。

²  获取、修改在APP domain中的设置项值。

²  删除设置项值。

²  检查可能被修改设置项的内容。

2.4.1         为APP注册默认设置项

默认情况下,为设置项提供0值的初值。如果不想用标准默认值,可以通过 registerDefaults: 方法指定默认值,它把默认值放入 NSRegistrationDomain域中。

通过registerDefaults:传入Dictionary提供所有所有需要注册的默认值,你可以在任意时刻注册,但要在使用设置项之前。

示例代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   // Register the preference defaults early.

    NSDictionary *appDefaults = [NSDictionary

        dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"CacheDataAgressively"];

    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];

 

   // Other initialization...

}

 

2.4.2         获得、修改设置项值

使用NSUserDefaults类的方法来获取、修改设置项的值,

获取示例:

[[NSUserDefaults standardUserDefaults]boolForKey:@"CacheDataAggressively"]

修改示例:

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

[defaults setBool:YES forKey:@"CacheDataAggressively"];

[defaults setObject:[NSDate dateWithTimeIntervalSinceNow:(3600 * 24* 7)]

forKey:@"CacheExpirationDate"];

[defaults removeObjectForKey:@"CacheExpirationDate"];

 

2.4.3         同步和监听设置项值的变化

因为NSUserDefaults会缓存值,有时候有必要把缓存值和user defaults database进行同步,你的APP不总是唯一会修改userdefaults database的实体。

IOS中,系统Settings可以为有Settings bundle的APP修改设置项值。

NSUserDefaults对象会自动的以一段时间循环同步设置项值,APP也可以手动调用synchronize方法强制同步。

可以注册NSUserDefaultsDidChangeNotification通知事件来监听设置项值的变化。

 

2.4.4         使用CocoaBindings管理设置项

Mac OS支持,略

 

2.4.5         使用CoreFundation管理设置项

可以使用CoreFundation中的方法完成,获取、设置、同步设置项值的操作。(它甚至可以修改其它应用的设置项,需要Root权限等限制。)

使用CoreFundation修改设置项

设置项是通过key/value对保存的,key必须为CFString对象,值的类型同上。

示例:

CFStringRef textColorKey = CFSTR("defaultTextColor");

CFStringRef colorBLUE = CFSTR("BLUE");

 

// Set up the preference.

CFPreferencesSetAppValue(textColorKey, colorBLUE,

        kCFPreferencesCurrentApplication);

 

// Write out the preference data.

CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);

使用CoreFundation获取值

示例:

CFStringRef textColorKey = CFSTR("defaultTextColor");

CFStringRef textColor;

 

// Read the preference.

textColor = (CFStringRef)CFPreferencesCopyAppValue(textColorKey,

        kCFPreferencesCurrentApplication);

// When finished with value, you must release it

// CFRelease(textColor);

 

2.5  用iCloud保存preference

暂时略过

2.6  实现IOS的Settings Bundle

IOS中,FoundationFramework为设置项数据的保存提供了底层机制,APP可以通过以下两种可选方式展示设置项:

²  在APP内部展示设置项。

²  使用Settings Bundle管理从系统Settings中来的设置项。

无论怎样显示设置项,使用NSUserDefaults获取、修改设置项。

 

2.6.1         系统Settings的用户界面

如下是其中可以使用的控件列表:

Control type

Description

Text field

The text field type displays a title (optional) and an editable text field. You can use this type for preferences that require the user to specify a custom string value.

The key for this type is PSTextFieldSpecifier.

Title

The title type displays a read-only string value. You can use this type to display read-only preference values. (If the preference contains cryptic or nonintuitive values, this type lets you map the possible values to custom strings.)

The key for this type is PSTitleValueSpecifier.

Toggle switch

The toggle switch type displays an ON/OFF toggle button. You can use this type to configure a preference that can have only one of two values. Although you typically use this type to represent preferences containing Boolean values, you can also use it with preferences containing non-Boolean values.

The key for this type is PSToggleSwitchSpecifier.

Slider

The slider type displays a slider control. You can use this type for a preference that represents a range of values. The value for this type is a real number whose minimum and maximum value you specify.

The key for this type is PSSliderSpecifier.

Multivalue

The multivalue type lets the user select one value from a list of values. You can use this type for a preference that supports a set of mutually exclusive values. The values can be of any type.

The key for this type is PSMultiValueSpecifier.

Group

The group type is for organizing groups of preferences on a single page. The group type does not represent a configurable preference. It simply contains a title string that is displayed immediately before one or more configurable preferences.

The key for this type is PSGroupSpecifier.

Child pane

The child pane type lets the user navigate to a new page of preferences. You use this type to implement hierarchical preferences. For more information on how you configure and use this preference type, see “Hierarchical Preferences.”

The key for this type is PSChildPaneSpecifier.

 

2.6.2         SettingsBundle

Settings Bundle有一个对应的Settings.bundle文件在APP Bundle目录下,它里面包含设置项页面的描述文件,其内容如下:

tem name

Description

Root.plist

The Settings page file containing the preferences for the root page. The name of this file must be Root.plist. The contents of this file are described in more detail in“The Settings Page File Format.”

Additional .plistfiles

If you build a set of hierarchical preferences using child panes, the contents for each child pane are stored in a separate Settings page file. You are responsible for naming these files and associating them with the correct child pane.

One or more .lprojdirectories

These directories store localized string resources for your Settings page files. Each directory contains a single strings file, whose title is specified in your Settings page file. The strings files provide the localized strings to display for your preferences.

Additional images

If you use the slider control, you can store the images for your slider in the top-level directory of the bundle.

 

APP Bundle里面还可以放入一个图标,用来在系统设置中标识APP。

 

载入机制

当系统Settings启动时,它会检查所有用户APP是否存在SettingsBundle。然后加载所有找到的bundle,并且在系统Settings中显示对应的APP名称和APP图标。

当用户点击了你的APP的行,系统Settings就会载入你的SettingsBundle中的Root.plist文件来创建你的APP的设置主页。当然,还会载入其它相关的资源,比如语言相关的资源。

 

Settings页面的文件格式

它是使用plist文件格式的,文件中包含的根元素如下表,只有第一个key是必须的,不过建议都包含。

Key

Type

Value

PreferenceSpecifiers(required)

Array

The value for this key is an array of dictionaries, with each dictionary containing the information for a single control. For a list of control types, see Table 4-1. For a description of the keys associated with each control, see Settings Application Schema Reference.

StringsTable

String

The name of the strings file associated with this file. A copy of this file (with appropriate localized strings) should be located in each of your bundle’s language-specific project directories. If you do not include this key, the strings in this file are not localized. For information on how these strings are used, see “Localized Resources.”

 

设置项的层级关系

主界面一定对应Root.plist文件,需要新设置页面就要加入新的.plist文件,名字可以自定。

使用Childpane控件来实现父页面和子页面的链接。

在Childpane中:

File Key指定子页面的.plist文件名。

Title Key指定子页面标题,也是父页面中列表行中的内容。

结构如下图:


本地化文件

每个支持的语言版本都会对应一个.strings文件,这和APP国际化的思路相同。

 

2.6.3         创建和修改SettingsBundle

XCode提供了创建SettingsBundle的模板,它默认生成Root.plist和本地化资源文件夹和其中的.string。

XCode中操作部分略,很简单。

 

原创粉丝点击