我有一个iOS应用程序,它一直使用NSUserDefaults保存用户默认值,但是,突然之间,应用程序不再保存设备上的默认值,但它在模拟器中完美运行。
以下是我要保存设置的方法
-(void)saveSettings{
defaults = [NSUserDefaults standardUserDefaults];
settingsData = [defaults objectForKey:(NSString *)@"SettingsKey"];
settingsDict = [NSKeyedUnarchiver unarchiveObjectWithData:settingsData];
NSMutableDictionary * settingsMutableDictionary = [settingsDict mutableCopy];
[settingsMutableDictionary setValue: [NSNumber numberWithBool:_dawnAlarmSwitch.on] forKey: @"dawnAlarmSwitch"];
[settingsMutableDictionary setValue:[NSNumber numberWithBool:_sunriseAlarmSwitch.on] forKey: @"sunriseAlarmSwitch"];
[settingsMutableDictionary setValue:[NSNumber numberWithBool:_noonAlarmSwitch.on] forKey: @"noonAlarmSwitch"];
[settingsMutableDictionary setValue:[NSNumber numberWithBool:_afternoonAlarmSwitch.on] forKey: @"afternoonAlarmSwitch"];
[settingsMutableDictionary setValue:[NSNumber numberWithBool:_sunsetAlarmSwitch.on] forKey: @"sunsetAlarmSwitch"];
[settingsMutableDictionary setValue:[NSNumber numberWithBool:_nightAlarmSwitch.on] forKey: @"nightAlarmSwitch"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_dawnAlarmDelaySlider.value] forKey: @"dawnAlarmDelaySlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_noonAlarmDelaySlider.value] forKey: @"noonAlarmDelaySlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_afternoonAlarmDelaySlider.value] forKey: @"afternoonAlarmDelaySlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_sunsetAlarmDelaySlider.value] forKey: @"sunsetAlarmDelaySlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_nightAlarmDelaySlider.value] forKey: @"nightAlarmDelaySlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_dawnSnoozeIntervalSlider.value] forKey: @"dawnSnoozeIntervalSlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_noonSnoozeIntervalSlider.value] forKey: @"noonSnoozeIntervalSlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_afternoonSnoozeIntervalSlider.value] forKey: @"afternoonSnoozeIntervalSlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_sunsetSnoozeIntervalSlider.value] forKey: @"sunsetSnoozeIntervalSlider"];
[settingsMutableDictionary setValue:[NSNumber numberWithInt:_nightSnoozeIntervalSlider.value] forKey: @"nightSnoozeIntervalSlider"];
// CFShow((__bridge CFTypeRef)(settingsDict));
settingsDict = [settingsMutableDictionary copy];
settingsData = [NSKeyedArchiver archivedDataWithRootObject:settingsDict];
[defaults setObject:settingsData forKey:(NSString *)@"SettingsKey"];
[defaults synchronize];
kvStore = [NSUbiquitousKeyValueStore defaultStore];
[kvStore setObject:settingsData forKey:(NSString *)@"SettingsKey"];
[kvStore synchronize];
}
以下是检索设置的方法:
-(void)getSettings{
kvStore = [NSUbiquitousKeyValueStore defaultStore];
settingsData = [kvStore objectForKey:(NSString *)@"SettingsKey"];
if (!settingsData){
defaults= [NSUserDefaults standardUserDefaults];
settingsData = [defaults objectForKey:(NSString *)@"SettingsKey"];
}
if (!settingsData)
[self createSettings];
if (settingsData){
settingsDict = [NSKeyedUnarchiver unarchiveObjectWithData:settingsData];
_showArabicQuranSwitch.on= [[settingsDict objectForKey:@"showArabicQuranSwitch"] boolValue];
_dawnAlarmSwitch.on= [[settingsDict objectForKey:@"dawnAlarmSwitch"] boolValue];
_sunriseAlarmSwitch.on= [[settingsDict objectForKey:@"sunriseAlarmSwitch"] boolValue];
_noonAlarmSwitch.on= [[settingsDict objectForKey:@"noonAlarmSwitch"] boolValue];
_afternoonAlarmSwitch.on= [[settingsDict objectForKey:@"afternoonAlarmSwitch"] boolValue];
_sunsetAlarmSwitch.on= [[settingsDict objectForKey:@"sunsetAlarmSwitch"] boolValue];
_nightAlarmSwitch.on= [[settingsDict objectForKey:@"nightAlarmSwitch"] boolValue];
_dawnAlarmDelaySlider.value= [[settingsDict objectForKey:@"dawnAlarmDelaySlider"] intValue];
_noonAlarmDelaySlider.value= [[settingsDict objectForKey:@"noonAlarmDelaySlider"] intValue];
_afternoonAlarmDelaySlider.value= [[settingsDict objectForKey:@"afternoonAlarmDelaySlider"] intValue];
_sunsetAlarmDelaySlider.value= [[settingsDict objectForKey:@"sunsetAlarmDelaySlider"] intValue];
_nightAlarmDelaySlider.value= [[settingsDict objectForKey:@"nightAlarmDelaySlider"] intValue];
_dawnSnoozeIntervalSlider.value= [[settingsDict objectForKey:@"dawnSnoozeIntervalSlider"] intValue];
if(self.dawnAlarmDelaySlider.value <0.0f)
_dawnAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins before", _dawnAlarmDelaySlider.value*-1];
else
_dawnAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins after", _dawnAlarmDelaySlider.value];
_noonSnoozeIntervalSlider.value= [[settingsDict objectForKey:@"noonSnoozeIntervalSlider"] intValue];
if(self.noonAlarmDelaySlider.value<0.0f)
_noonAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins before", _noonAlarmDelaySlider.value*-1];
else
_noonAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins after", _noonAlarmDelaySlider.value];
_afternoonSnoozeIntervalSlider.value= [[settingsDict objectForKey:@"afternoonSnoozeIntervalSlider"] intValue];
if(self.afternoonAlarmDelaySlider.value<0.0f)
_afternoonAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins before", _afternoonAlarmDelaySlider.value*-1];
else
_afternoonAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins after", _afternoonAlarmDelaySlider.value];
_sunsetSnoozeIntervalSlider.value= [[settingsDict objectForKey:@"sunsetSnoozeIntervalSlider"] intValue];
if(self.sunsetAlarmDelaySlider.value<0.0f)
_sunsetAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins before", _sunsetAlarmDelaySlider.value*-1];
else
_sunsetAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins after", _sunsetAlarmDelaySlider.value];
_nightSnoozeIntervalSlider.value= [[settingsDict objectForKey:@"nightSnoozeIntervalSlider"] intValue];
if(self.nightAlarmDelaySlider.value<0.0f)
_nightAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins before", _nightAlarmDelaySlider.value*-1];
else
_nightAlarmDelayLabel.text = [NSString stringWithFormat:@"%.0f mins after", _nightAlarmDelaySlider.value];
_dawnSnoozeIntervalLabel.text = [NSString stringWithFormat:@"%.0f mins", _dawnSnoozeIntervalSlider.value];
_noonSnoozeIntervalLabel.text = [NSString stringWithFormat:@"%.0f mins", _noonSnoozeIntervalSlider.value];
_afternoonSnoozeIntervalLabel.text = [NSString stringWithFormat:@"%.0f mins", _afternoonSnoozeIntervalSlider.value];
_sunsetSnoozeIntervalLabel.text = [NSString stringWithFormat:@"%.0f mins", _sunsetSnoozeIntervalSlider.value];
_nightSnoozeIntervalLabel.text = [NSString stringWithFormat:@"%.0f mins", _nightSnoozeIntervalSlider.value];
}
// CFShow((__bridge CFTypeRef)(settingsDict));
}
-(void)createSettings{
settingsDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"showArabicQuranSwitch",
[NSNumber numberWithBool:NO], @"showArabicQuranSwitch",
[NSNumber numberWithBool:YES], @"dawnAlarmSwitch",
[NSNumber numberWithBool:NO], @"sunriseAlarmSwitch",
[NSNumber numberWithBool:YES], @"noonAlarmSwitch",
[NSNumber numberWithBool:YES], @"afternoonAlarmSwitch",
[NSNumber numberWithBool:YES], @"sunsetAlarmSwitch",
[NSNumber numberWithBool:YES], @"nightAlarmSwitch",
[NSNumber numberWithInt:10], @"dawnSnoozeIntervalSlider",
[NSNumber numberWithInt:10], @"noonSnoozeIntervalSlider",
[NSNumber numberWithInt:10], @"afternoonSnoozeIntervalSlider",
[NSNumber numberWithInt:10], @"sunsetSnoozeIntervalSlider",
[NSNumber numberWithInt:10], @"nightSnoozeIntervalSlider",
[NSNumber numberWithInt:0], @"dawnAlarmDelaySlider",
[NSNumber numberWithInt:0], @"noonAlarmDelaySlider",
[NSNumber numberWithInt:0], @"afternoonAlarmDelaySlider",
[NSNumber numberWithInt:0], @"sunsetAlarmDelaySlider",
[NSNumber numberWithInt:0], @"nightAlarmDelaySlider",
nil];
settingsData = [NSKeyedArchiver archivedDataWithRootObject:settingsDict];
[defaults setObject:settingsData forKey:(NSString *)@"SettingsKey"];
[defaults synchronize];
}
提前致谢。
答案 0 :(得分:0)
我改变了这一行
if (!settingsData)
[self createSettings];
到
settingsDict = [NSKeyedUnarchiver unarchiveObjectWithData:settingsData];
if (!settingsData || !settingsDict)
[self createSettings];
一切正常。