Question

I am using the a preference(plist) file to save the user information. this file is stored in the preference folder of the app, For some logged in users this preference file is restored to default values when they switch off the ipad and restart it next morning. Any ideas or thought on why is this occurring.

We are reading the plist like this

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

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

And after saving we write it off with

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

some more informations... does this have any thing to do with this automatic restore. i am seeing this line in the ipads who's plist was restored..

<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.
Was it helpful?

Solution

I m sure you are writing your plist file that is there in bundle.Apple documentation says "It is not recommended to modify your bundle content after application code signed". when you build your application again it will be overridden by default plist file what you have it in your project.

You should copy your default plist file in document directory and access it for writing. this will even go off when you delete the application from device.

To copy your plist to document directory follow

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];
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top