InAppSettingsKit 用法

来源:互联网 发布:centos 6.7 破解密码 编辑:程序博客网 时间:2024/05/01 00:18

============================================================
博文原创,转载请声明出处
电子咖啡(原id蓝岩)
============================================================
InAppSettingsKit 是一款功能强大的ios设置组件,可以满足各种各样的app设置需求。下载

下载解压,“InAppSettingsKit”为工具包,再app中引入即可使用。InAppSettingsKitSampleApp 是demo。

***

下面分析InAppSettingsKit使用方法。

在项目中引入InAppSettingsKit包。

创建setting.bundle,其中默认为Root.plist,是再ios setting app中显示的设置配置文件。而InAppSettingsKit可以再app中自定义文件也需要对应的配置文件即Root.inApp.plist,如果没有此文件,app将默认使用Root.plist,其具体选择order为:

- InAppSettings.bundle/FILE~DEVICE.inApp.plist- InAppSettings.bundle/FILE.inApp.plist- InAppSettings.bundle/FILE~DEVICE.plist- InAppSettings.bundle/FILE.plist- Settings.bundle/FILE~DEVICE.inApp.plist- Settings.bundle/FILE.inApp.plist- Settings.bundle/FILE~DEVICE.plist- Settings.bundle/FILE.plist

在xx.plist中通过dictionary来配置设置信息,其中InAppSettingsKit扩展了type,如下:

IASKOpenURLSpecifier--------------------InAppSettingsKit adds a new element that allows to open a specified URL using an external application (i.e. Safari or Mail). See the sample `Root.inApp.plist` for details.IASKMailComposeSpecifier------------------------The custom `IASKMailComposeSpecifier` element allows to send mail from within the app by opening a mail compose view. You can set the following (optional) parameters using the settings plist: `IASKMailComposeToRecipents`, `IASKMailComposeCcRecipents`, `IASKMailComposeBccRecipents`, `IASKMailComposeSubject`, `IASKMailComposeBody`, `IASKMailComposeBodyIsHTML`. Optionally, you can implement    - (NSString*)settingsViewController:(id<IASKViewController>)settingsViewController mailComposeBodyForSpecifier:(IASKSpecifier*)specifier;in your delegate to pre-fill the body with dynamic content (great to add device-specific data in support mails for example). An alert is displayed if Email is not configured on the device. `IASKSpecifier` is the internal model object defining a single settings cell. Important IASKSpecifier properties:- `key`: corresponds to the `Key` in the Settings plist- `title`: the localized title of settings key- `type`: corresponds to the `Type` in the Settings plist- `defaultValue`: corresponds to the `DefaultValue` in the Settings plistIASKButtonSpecifier // 自定义type-------------------InAppSettingsKit adds a `IASKButtonSpecifier` element that allows to call a custom action. Just add the following delegate method: //这里记录了action,后面的描述类似    - (void)settingsViewController:(IASKAppSettingsViewController*)sender buttonTappedForSpecifier:(IASKSpecifier*)specifier;The sender is always an instance of `IASKAppSettingsViewController`, a `UIViewController` subclass. So you can access its view property (might be handy to display an action sheet) or push another view controller. Another nifty feature is that the title of IASK buttons can be overriden by the (localizable) value from `NSUserDefaults` (or any other settings store - see below). This comes in handy for toggle buttons (e.g. Login/Logout). See the sample app for details.By default, Buttons are aligned centered except if an image is specified (default: left-aligned). The default alignment may be overridden.FooterText----------The FooterText key for Group elements is available in system settings since iOS 4. It is supported in InAppSettingsKit as well. On top of that, we support this key for Multi Value elements as well. The footer text is displayed below the table of multi value options.IASKCustomViewSpecifier //custome view-----------------------You can specify your own `UITableViewCell` within InAppSettingsKit by using the type `IASKCustomViewSpecifier`. A mandatory field in this case is the `Key` attribute. Also, you have to support the `IASKSettingsDelegate` protocol and implement these methods:    - (CGFloat)tableView:(UITableView*)tableView heightForSpecifier:(IASKSpecifier*)specifier;    - (UITableViewCell*)tableView:(UITableView*)tableView cellForSpecifier:(IASKSpecifier*)specifier;Both methods are called for all your `IASKCustomViewSpecifier` entries. To differentiate them, you can access the `Key` attribute using `specifier.key`. In the first method you return the height of the cell, in the second method the cell itself. You should use reusable `UITableViewCell` objects as usual in table view programming. There's an example in the Demo app.Optionally you can implement    - (void)settingsViewController:(IASKAppSettingsViewController*)sender tableView:(UITableView *)tableView didSelectCustomViewSpecifier:(IASKSpecifier*)specifier;to catch tap events for your custom view.Custom Group Header Views-------------------------You can define custom headers for `PSGroupSpecifier` segments by adding a `Key` attribute and implementing these methods in your `IASKSettingsDelegate`:    - (CGFloat)settingsViewController:(id<IASKViewController>)settingsViewController tableView:(UITableView*)tableView heightForHeaderForSection:(NSInteger)section;    - (UIView*)settingsViewController:(id<IASKViewController>)settingsViewController tableView:(UITableView *)tableView viewForHeaderForSection:(NSString*)key;The behaviour is similar to the custom cells except that the methods get the key directly as a string, not via a `IASKSpecifier` object. (The reason being that custom group header views are meant to be static.) Again, check the example in the demo app.Custom ViewControllers //Advanced----------------------For child pane elements (`PSChildPaneSpecifier`), Apple requires a `file` key that specifies the child plist. InAppSettingsKit allow to alternatively specify `IASKViewControllerClass` and `IASKViewControllerSelector`. In this case, the child pane is displayed by instantiating a UIViewController subclass of the specified class and initializing it using the init method specified in the `IASKViewControllerSelector`. The custom view controller is then pushed onto the navigation stack. See the sample app for more details.Text alignment--------------For some element types, a `IASKTextAlignment` attribute may be added with the following values to override the default alignment:- `IASKUITextAlignmentLeft` (Buttons, TitleValue, MultiValue, OpenURL, TextField)- `IASKUITextAlignmentCenter` (Buttons, OpenURL)- `IASKUITextAlignmentRight` (Buttons, TitleValue, MultiValue, OpenURL, TextField)Variable font size------------------By default, the labels in the settings table are displayed in a variable font size, especially handy to squeeze-in long localizations (beware: this might break the look in Settings.app if labels are too long!).To disable this behavior, add a `IASKAdjustsFontSizeToFitWidth` Boolean attribute with value `NO`.Icons-----All element types (except sliders which already have a `MinimumValueImage`) support an icon image on the left side of the cell. You can specify the image name in an optional `IASKCellImage` attribute. The ".png" or "@2x.png" suffix is automatically appended and will be searched in the project. Optionally, you can add an image with suffix "Highlighted.png" or "Highlighted@2x.png" to the project and it will be automatically used as a highlight image when the cell is selected (for Buttons and ChildPanes).Settings Storage----------------The default behaviour of IASK is to store the settings in ``[NSUserDefaults standardUserDefaults]`. However, it is possible to change this behavior by setting the `settingsStore` property on an `IASKAppSettingsViewController`. IASK comes with two store implementations: `IASKSettingsStoreUserDefaults` (the default one) and `IASKSettingsStoreFile`, which read and write the settings in a file of the path you choose. If you need something more specific, you can also choose to create your own store. The easiest way to create your own store is to create a subclass of `IASKAbstractSettingsStore`. Only 3 methods are required to override. See `IASKSettingsStore.{h,m}` for more details.Notifications-------------There's a `kIASKAppSettingChanged` notification that is sent for every changed settings key. The `object` of the notification is the userDefaults key (NSString*). The `userInfo` dictionary contains the new value of the key.Dynamic cell hiding-------------------Sometimes, options depend on each other. For instance, you might want to have an "Auto Connect" switch, and let the user set username and password if enabled. To react on changes of a specific setting, use the `kIASKAppSettingChanged` notification explained above.To hide a set of cells use:    - (void)[IASKAppSettingsViewController setHiddenKeys:(NSSet*)hiddenKeys animated:(BOOL)animated];or the non-animated version:@property (nonatomic, retain) NSSet *hiddenKeys;See the sample app for more details.Subclassing notes-----------------If you'd like to customize the appearance of InAppSettingsKit, you might want to subclass `IASKAppSettingsViewController` and override some `UITableViewDataSource` or `UITableViewDelegate` methods. If you do subclass, make sure to override the `-initWithNibName:bundle:` method in any case:    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        return [super initWithNibName:@"IASKAppSettingsView" bundle:nibBundleOrNil];
    }



其中还记录了InAppSettingsKit自定义扩展的一些action或者view。具体使用方法参见demo中MainViewController.m的实现。