IOS設置項相關----Preferences and Settings Programming Guide

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的組織結構

每個設置項由三部分構成:

2  它所在在的域,比如APP專有設置項、系統設置項。

2  它的名字,是一個NSString對象。

2  它的值, (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方法獲取,可以用它:

2  在APP啓動時,爲APP設置項指定默認值。

2  獲取、修改在APP domain中的設置項值。

2  刪除設置項值。

2  檢查可能被修改設置項的內容。

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可以通過以下兩種可選方式展示設置項:

2  在APP內部展示設置項。

2  使用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 p_w_picpaths

If you use the slider control, you can store the p_w_picpaths 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中操作部分略,很簡單。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章