我需要将数据保存到我的 iOS 自定义键盘中的 NSUserDefault
。
我已在开发人员门户和 XCode
中成功创建应用组和授权。
这是我如何使用 UISwitch 保存我的值
- (IBAction)switchAction:(id)sender
{
[self.defaults setBool:self.myKey.on forKey:@"myValue"];
[self.defaults synchronize];
}
这是我在 KeyboardView 中加载值的方式。
self.defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.customkeyboard"];
if([self.defaults boolForKey:@"myValue"])
{
self.backgroundColor = [UIColor redColor];
}
else
{
self.backgroundColor = [UIColor blackColor];
}
它不起作用,也不会加载值。
如何保存和加载数据?
最佳答案
在应用组中的所有应用程序中像这样初始化您的 NSUserDefaults
对象,它们将共享数据库:
[[NSUserDefaults alloc] initWithSuiteName:@"group identifier"];
请记住,每个应用程序的 [NSUserDefaults standardUserDefaults]
数据库中的所有内容都不会转移到此数据库中。
documentation indicates [NSUserDefaults standardUserDefaults]
应该像上面的代码一样返回共享数据库,但它没有。我将其作为错误 (rdar://17164758) 提交。
不要忘记同步数据库:
[yourDefaults synchronize];
关于ios - NSUserDefault 无法在 iOS8 的扩展中保存和加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24244126/