So in my app I have a bunch of data that I'd like to write to a log file, and then display it within a UITextView
when I click a button. I know how to toggle the UITextView, but I have no idea how to create and update a log file (in the local filesystem). Thanks for any help.
Asked
Active
Viewed 1.9k times
12

Julian Coltea
- 3,599
- 10
- 26
- 32
2 Answers
34
The basic idea is that you create the file, and append to it every time you log a new line. You can do it quite easily like this:
Writing to the file:
NSString *content = @"This is my log";
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"myFileName.txt"];
//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];
//append text to file (you'll probably want to add a newline every write)
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file seekToEndOfFile];
[file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
Reading:
//get file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"myFileName.txt"];
//read the whole file as a single string
NSString *content = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];

CrimsonDiego
- 3,616
- 1
- 23
- 26
-
It doesn't like `[NSFileHandle fileHandleForUpdatingAtPath:logPath];` saying that `logPath` is use of undeclared identifier. Could you please clarify it? – Julian Coltea Jun 15 '12 at 20:28
-
My bad - replace logPath with fileName. Wrong variable. – CrimsonDiego Jun 15 '12 at 20:29
-
1Thanks. Although I did everything you said, and then when I wanted to add the data to my UITextView, using `logFile.text = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];` Nothing showed up. I did some debugging and saw that the log file itself was null. Do you have any idea why? – Julian Coltea Jun 15 '12 at 21:09
-
1I just tested it myself, works fine. Make sure the variable `fileName` has the same value for reading and writing. Also, if you are testing on the simulator, go look in `~/Library/Application Support/iPhone Simulator/X.Y/Applications/ZZZZZ/Documents` where X.Y is the simulator version you ran it in (ie, 4.3, 5.0, etc) and ZZZZZ is your application id (you can see it if you NSLog fileName) and see if your file exists. This will tell you if you are creating it wrong, or are reading it wrong. – CrimsonDiego Jun 15 '12 at 21:21
1
I thought was a class out there to do this automatically as after no luck created my own.
NSLogger is a lightweight class for iOS versions 3.0 and above. It allows developers to easily log different 'events' over time which are locally stored as a .txt file.

Joe Barbour
- 842
- 9
- 14