My app writes an encrypted data of the user info/preferences into a file, and reads from that file the next time the app is opened.
Writing a file:
- (BOOL)writeFile:(NSString *)data:(NSString *)fileName {
return [data writeToFile:fileName
atomically:YES
encoding:NSUTF8StringEncoding error:nil];
}
Reading a file:
- (NSString *)readFile:(NSString *)fileName {
NSData *data = [NSData dataWithContentsOfFile:fileName];
NSString *str = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
return str;
}
This works fine on the emulator. The files are written and read as expected. Is there anything I have to setup for file read/write on devices?
The filename has to be in the documents directory. The simulator won't have as many restrictions on where it can write files as the device does.
Obtain the documents directory as follows:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"myfilename.extension"];
Pass this into your functions above and you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With