/*
读取系统保存在本地的运行参数。
参数以plist文件的形式保存在本地程序沙盒中。
读取之后暂时保存在setup数组中,程序运行当中实时的对该数据进行更改
在退出程序之前,将setup数组中的数据保存在本地
*/
- (void)getSetupFromLocal
{
if (setupDic==nil) {
setupDic = [[NSMutableDictionary alloc] initWithCapacity:0];
}
// 读取本地沙盒中的数据
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"setup.plist"];
// 判断路径是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:fileName]) {
setupDic = [NSMutableDictionary dictionaryWithContentsOfFile:fileName];
}
}
//通过键值获取设置的具体属性
- (NSString *)getSetupWithKey:(NSString *)key
{
if (setupDic && key) {
return [setupDic objectForKey:key];
}
return @"获取失败";
}
//保存到本地
- (void)saveSetupToLocal
{
if (setupDic) {
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"setup.plist"];
[setupDic writeToFile:fileName atomically:YES];
}
}
//设置字典数据
- (void)setSetup:(NSString *)value forKey:(NSString *)key
{
// 如果字典存在并且数据有效
if (setupDic && value && key) {
// 将该条数据删除之后重新加载
[setupDic removeObjectForKey:key];
[setupDic setObject:value forKey:key];
}
}