Chris
Chris

Reputation: 27394

iPhone: User data saves/loads in simulator, but not on iPhone

FI can save and load Dictionary data within the simulator but when I run it on my iPhone it doesn't seem to work. I cant quite tell what is happening. Here is how I save and load data:

Loading

NSString *homeDir = NSHomeDirectory();
NSDictionary *wholeDictionary = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@userdata.dat",homeDir]];

Saving

NSString *homeDir = NSHomeDirectory();
[dict writeToFile:[NSString stringWithFormat:@"%@userdata.dat",homeDir] atomically:YES];

Upvotes: 1

Views: 88

Answers (1)

rob mayoff
rob mayoff

Reputation: 386018

NSHomeDirectory returns the application directory, which is not writeable. You need to write your data to the application's documents directory:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES) objectAtIndex:0];
[dict writeToFile:[documentsPath stringByAppendingPathComponent:@"userdata.dat"] atomically:YES];

Upvotes: 2

Related Questions