I am trying to save the current date into my plist then commit the plist to the phone for later use (and update).
The code below is what I am using to try and save the date, however it dose not seem to be working as it should only ever enter “once” (once the file is created the first time) then skip this each time after.. as its been created and available….
However this is not working and I am not sure why, this is the code I have written;
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"DB-Date.plist"];
if(![[NSFileManager defaultManager] fileExistsAtPath:plistFilePath])
{//dosnt exits
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DB-Date" ofType:@"plist"];
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSArray *arrValue = [data objectAtIndex:0];
NSDate *dateValue = [arrValue objectAtIndex:0];
dateValue = [NSDate date]; //change your value here
[[data objectAtIndex:0] replaceObjectAtIndex:0 withObject:dateValue]; //value replaced
[data writeToFile:plistFilePath atomically:YES];
}
// from here I continue to use the newly created plist file
any help would be greatly appreciated.
So, mainBundle and NSDocumentaryDirectory are two different places…This should be enough to solve your problem 😉
You seem to have a lot of excess code…Let’s do this instead in the brackets:
If you like the answer please mark as correct. Thanks!