Does iOS Preference file automatically reset to default values - ios

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.

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];
}

Related

Will property list data persist after killing the application?

I have created a custom property list file. The file is stored in the application document.
While user login is successful the login information is stored in the plist, and it is working fine.
The plist content are cleared while log out, this also works fine.
When i am still login i killed the application. When the app opens the plist data i cleared.
code used to save to file:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"xxxxPlist.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"xxxxPlist" ofType:#"plist"];
}
dict=[[self cleanDictionary:[dict mutableCopy]] mutableCopy];
NSDictionary *plistDict=[[NSDictionary alloc] initWithObjectsAndKeys:dict,#"login_data", nil];
NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
}
else
{
//error here
NSLog(#"%# ",error);
}
code used to fetch data
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"xxxx.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"xxxxPlist" ofType:#"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
return [dict objectForKey:#"login_data"];
I there any way out to persist the data?
There are several things that may be causing problems
When saving to file
1) My understanding is that you specifically want to save to /Documents folder specifically to ensure your file persists
2) So you correctly build following path
"/Documents/xxxxPlist.plist"
3) But then why do you check if a file already exists at that location?
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"xxxxPlist" ofType:#"plist"];
}
You just have to write to the path when you are ready.
If there is an old file at this location it will be overwritten.
And my understanding is this is the wanted behaviour, because you've already read that file and the data is in that dictionary "dict".
4) Also, by asking NSBundle to give you path for your file name
"xxxxPlist.plist"
there is a risk that it will just give you back some other path with file named the same (not in /Documents) if such file happens to exist. For example if you happened to write to say, /Cache folder earlier (with different code), your app will keep getting the /Cache path and keep reading/writong there (not in /Documents). And with the existing code you would have gotten nil here for path on the very first run, so not sure how he file got created in the first place.
5) Then I am not sure what exactly does this line
dict=[[self cleanDictionary:[dict mutableCopy]] mutableCopy];
Why first make a mutable copy, then presumably get immutable copy back and get a mutable one of it. Can't -cleanDictionary: just return the same mutable copy it was passed?
When reading from file
1) Not sure why you're searching for a different file first?
"/Documents/xxxx.plist" not "/Documents/xxxxPlist.plist"
Also what happens if "xxxx.plist" exists, then you'll never get to "xxxxPlist.plist" that you are writing in the other section.
2) Then, yes, you have to check if a file exists at certain path before you try to read it. But, in your case, if it does not exist, you don't ask NSBundle for another location, because you need your specific file in /Documents, and you don't know what you'll get from NSBundle, if your file is not where it should be.
So if there is no "xxxxPlist.plist" file, it's just your first run of the app and you will be creating your initial dict.

Why can't I retrieve my plist file?

I have a plist file I just created of strings; it looks like this:
This is the code I'm using to create the path to the file:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Create a list of paths
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get a path to your documents directory from the list
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"NailServices.plist"]; // Create a full file path
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) { // Check if file exists
// Get a path to the plist created before in bundle directory (by Xcode)
NSString *bundle = [[NSBundle mainBundle] pathForResource: #"NailServices" ofType: #"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error]; // Copy this plist to your documents directory
}
This is the code I'm using to examine the data (to make sure this is working)... I'm getting a (null) back from the NSLog statement)
//Load Dictionary with wood name cross refference values for image name
NSString *plistDataPath = [[NSBundle mainBundle] pathForResource:#"NailServices" ofType:#"plist"];
NSDictionary *NailServicesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistDataPath];
NSLog(#"\nnailServicesDict: %#", NailServicesDictionary);
This is my first attempt at creating/using a "strings" plist file; I have read everything I could find on Google and SO without finding an example of a plain ol' strings file. What else do I have to do to be able to get to this plist data?
Your problem is that you are creating an NSDictionary while your plist is an NSArray. Thus, it will return nil when you try to create it as a dictionary because no dictionary exists.
You need to change:
NSDictionary *NailServicesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistDataPath];
to
NSArray *NailServicesArray = [[NSArray alloc] initWithContentsOfFile:plistDataPath];
As a commenter posted, plist files can either have an NSArray or an NSDictionary as their root. Your example plist has an NSArray as its root, so you'll want to alloc and init an NSArray, not an NSDictionary. If your plist is stored in the app bundle when you build the app in Xcode and you don't need to modify it at runtime, then it's unnecessary to copy it to the NSDocumentsDirectory. Also, I'd recommend using [paths lastObject]; rather than [paths objectAtIndex:0];, which can throw an exception if the paths array is empty.

I have uploaded application with core data and I want to replace new core data without migration. Does apple allows it?

I want to replace core data and want to delete old core data. Does Apple allows it to delete old core data. Is there any chance to reject app.
Apple won't care, but your users might. If there is data that your users might be sorry to lose, you should make every effort to migrate it or give an option to export it when they upgrade.
If you're only using core data to cache downloaded values, then there's no problem with this at all. In fact, deleting the old store would be necessary to prevent the app crashing on upgrade, since it wouldn't be able to migrate from the existing store. The best place to do this is in the core data setup code when you receive an error - the boilerplate comments guide you toward this.
There are no problems doing that.
For example, you can check app's version in the AppDelegate:
- (void)clearCacheIfNeeded;
{
NSString *savedVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"currentVersion"];
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
if (![savedVersion isEqualToString:currentVersion]) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:cachesDirectory error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {
NSLog(#"file name: %#", filename);
[fileManager removeItemAtPath:[cachesDirectory stringByAppendingPathComponent:filename] error:NULL];
}
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"currentVersion"];
}
}

.plist path on iOS device

-(void)login{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:#"login" ofType:#"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[plistDict setObject:#"si" forKey:#"stato"];
[plistDict writeToFile:path atomically: YES];
}
In iOS Simulator the plist has been correctly written, but when I try to write the .plist on my iPhone, it doesn't work. I guess it is because of the wrong .plist path.
Do the iOS devices use different path?
First you have to check if the file exits in your documents directory. If it doesn't exits there then you can copy it to the document directory. You can do it this way
-(void)login{
BOOL doesExist;
NSError *error;
NSString *filePath= [[NSBundle mainBundle] pathForResource:#"login" ofType:#"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:#"login.plist"]];
doesExist= [fileManager fileExistsAtPath:path];
if (doesExist) {
NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else
{
doesExist= [fileManager copyItemAtPath:filePath toPath:path error:&error];
NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}
[plistDict setObject:#"si" forKey:#"stato"];
[plistDict writeToFile:path atomically: YES];
}
You can't write to the [NSBundle mainBundle] location. In order to write files like a plist, you should save in the documents folder, this way:
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *filePathToSave = [arrayPaths objectAtIndex:0];
If the plist is part of your app, I would recommend you, in the first launch, to already copy it to the documents folder using the same filePathToSave, so you will always look at it there, both to read or to save.
This is a big mistake, as the main bundle only is readable and only composed at compile time in the App Bundle. The App Bundle lives in a separate place, whereas the data you should write to disk should be placed into the Documents, Temporary or Library folder of your sandbox.
To gain more understanding please read the official File System Programming Guide.
Everything you need to know is written there.
You can also write to subfolders and you should choose between the 3 above mentioned main directories in terms of backing up, when syncing with iTunes or iCloud. For instance contents in the tmp Folder won't be backed up.
You can not write to the mainBundle on an iOS device. You will have to save the file to a directory and modify it there.
Just to bring the answers into the modern world - you should really be using the URL based methods for getting directories:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *URLForDocumentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]

Error says source path is nil

I am quite new to iOS app development.
I am trying to build a sample DB app which saves records to DB and fetches from it.
The App works well when I test with simulators. I have created a local database and also I am programatically making a copy of it when required. Also I have checked that local DB is referenced in the 'Copy Bundle Resources'.
But the error I am getting is, " * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil' ".
I think the piece of code which causes this problem is
" [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; "
It work perfectly good for simulators but not when I test with my device.
Expecting help.
My code is,
- (id)init {
self = [super init];
//Datbase Name
DBName=#"Person.sqlite3";
//Getting DB Path
NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPath objectAtIndex:0];
DBPath=[documentsDir stringByAppendingPathComponent:DBName ];
//DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]];
NSLog(#"DBPath is %#",DBPath);
return self;
}
-(void)checkAndCreateDB{
BOOL Success;
//NSFileManager maintains File
NSFileManager *FileManager = [NSFileManager defaultManager];
NSLog(#"line 1");
//Checks Database Path
Success = [FileManager fileExistsAtPath:DBPath];
NSLog(#"line 2");
//If file exists, it returns true
if(Success)return;
NSLog(#"line 3");
//NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
// Get the path to the database in the application package
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:#"Person.sqlite3" ofType:#"sqlite3"];
NSLog(#"line 4");
[FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
//[FileManager release];
NSLog(#"line 5");
}
My Application crashes while testing in device after the line NSLOG(line 4); But works good in simulator.
Many Thanks
Prakash
The device can not write to the app bundle, you need to use the Documents directory.
Here is how to get the Documents directory path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
If you have an initial db in the app bundle copy it to the Documents on initial startup.
change this line
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:#"Person.sqlite3" ofType:#"sqlite3"];
to
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:#"Person" ofType:#"sqlite3"];
also, pay attention to capitalisation, iOS devices are case sensitive, while on simulator, you'll get away with wrong capitalisation

Resources