要使用iOS偏好设置其实是个很简单的事情,你只要做到两点就可以了,
1.创建一个setting.Bundle
第一件事情就是在 xcode->file->New->File 中创建setting bundle 就可以了,如下图
这里介绍一下settingbundle 的内容
Root.plist 这个是主plist主列表的所在
.plist 其他子列表,用于多层设置的shihoushiyong
.lproj 用于设置本地化的东西
images,在设置列表中使用的图片
2.第二件事情就是设置 这和setting 的plist
通过设置里面的内容,使我们可以在系统界别的的setting中看到我们自己的应用设置,这样就可以直接动态设置一些默认的app参数
一共我们可以设置以下7种不同的参数Item,其中Group使用来显示设置分组情况的,当然每种不同的
| 设置控件 | 类型 |
|---|---|
| 文本框 | PSTextFieldSpecifier |
| 文字 | PSTitleValueSpecifier |
| 开关控件 | PSToggleSwitchSpecifier |
| Slider | PSSliderSpecifier |
| Multivalue | PSMultiValueSpecifier |
| Group | PSGroupSpecifier |
| 子面板 | PSChildPaneSpecifier. |
具体怎么写,我就用苹果官方的例子让大家看一下了
3.使用
使用这个跟使用NSUserDefaults的方法是一样的,直接取key就可以了,当然你可以通过方法,进行默认值的设置,比如下面
// Register the preference defaults early.
NSDictionary *appDefaults = [NSDictionary
dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:@"CacheDataAgressively"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];其他知识点
这里再跟大家说一下另一种可以跨设备的存储方式,存储到iCloud,能用于多设备同步,他就是
NSUbiquitousKeyValueStore ,它只能存储大概1M的内容,而且他的用法其实根NSUserDefaults是一样的,我们可以这么来监听它,以做到多设备信息的同步
NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateKVStoreItems:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:store];
[store synchronize];
- (void)updateKVStoreItems:(NSNotification*)notification {
// Get the list of keys that changed.
NSDictionary* userInfo = [notification userInfo];
NSNumber* reasonForChange = [userInfo
objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey];
NSInteger reason = -1;
// If a reason could not be determined, do not update anything.
if (!reasonForChange)
return;
// Update only for changes from the server.
reason = [reasonForChange integerValue];
if ((reason == NSUbiquitousKeyValueStoreServerChange) ||
(reason == NSUbiquitousKeyValueStoreInitialSyncChange)) {
// If something is changing externally, get the changes
// and update the corresponding keys locally.
NSArray* changedKeys = [userInfo
objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore]; NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// This loop assumes you are using the same key names in both
// the user defaults database and the iCloud key-value store
for (NSString* key in changedKeys) {
id value = [store objectForKey:key];
[userDefaults setObject:value forKey:key];
}
} }
这篇博客详细介绍了如何在iOS应用中实现偏好设置,包括创建Setting.Bundle、配置.plist文件以及如何使用设置内容。通过设置bundle,开发者可以将应用的个性化选项集成到系统设置中,实现动态设置默认参数。此外,还提到了使用NSUbiquitousKeyValueStore进行跨设备存储,以实现多设备信息同步。
88

被折叠的 条评论
为什么被折叠?



