【问题标题】:Does iOS Preference file automatically reset to default valuesiOS Preference 文件是否会自动重置为默认值
【发布时间】:2014-04-06 04:31:33
【问题描述】:

我正在使用首选项(plist)文件来保存用户信息。此文件存储在应用程序的首选项文件夹中,对于某些已登录的用户,当他们关闭 ipad 并在第二天早上重新启动时,此首选项文件会恢复为默认值。 关于为什么会发生这种情况的任何想法或想法。

我们正在这样阅读 plist

[[NSUserDefaults standardUserDefaults] registerDefaults:[AppSetting globalConfig]];

+ (NSDictionary *) globalConfig {
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
    return [[[NSDictionary alloc] initWithContentsOfFile:plistPath] autorelease];
}

保存后我们用

将其注销
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];   
[defaults setBool:self.isLogIn forKey:@"isLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];

更多信息...这与自动恢复有什么关系。我在 ipad 中看到这条线,谁的 plist 已恢复..

<Error>: HID: The 'Passive' connection 'appName' access to protected services is denied.
<Error>: HID: The 'Rate Controlled' connection 'appName' access to protected services is denied.

【问题讨论】:

标签: ios iphone ipad plist


【解决方案1】:

我确定您正在编写 bundle.Apple 文档中的 plist 文件 “不建议在应用程序代码签名后修改您的捆绑内容”。当您再次构建应用程序时,默认情况下它将被覆盖您在项目中拥有的 plist 文件。

你应该复制你的默认plist文件到文档目录并访问它进行写作。当您从设备中删除应用程序时,这甚至会消失。

要将您的 plist 复制到文档目录,请遵循

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"settings.plist"];

if ([fileManager fileExistsAtPath:txtPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
} 

【讨论】:

  • 这是否是第二天早上打开 ipad 后 plist 突然重置为默认状态的原因。
  • 并非如此。但是您正在修改不推荐的捆绑内容。
  • 将不得不拭目以待,因为不幸的是这个问题很少重现..
猜你喜欢
  • 1970-01-01
  • 2015-12-11
  • 2014-11-10
  • 2021-11-08
  • 2019-08-24
  • 2014-10-15
  • 2017-06-09
  • 1970-01-01
  • 2015-09-09
相关资源
最近更新 更多