Possibly not what you want, but I use this:
- (void)logIt:(NSString *)string {
// First send the string to NSLog
NSLog(@"%@", string);
// Setup date stuff
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-dd-MM"];
NSDate *date = [NSDate date];
// Paths - We're saving the data based on the day.
NSString *path = [NSString stringWithFormat:@"%@-logFile.txt", [formatter stringFromDate:date]];
NSString *writePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:path];
// We're going to want to append new data, so get the previous data.
NSString *fileContents = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:nil];
// Write it to the string
string = [NSString stringWithFormat:@"%@\n%@ - %@", fileContents, [formatter stringFromDate:date], string];
// Write to file stored at: "~/Library/Application\ Support/iPhone\ Simulator/*version*/Applications/*appGUID*/Documents/*date*-logFile.txt"
[string writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
This writes the data into a file on your device (a daily file). If you want it to reset after each session you can surely modify the code to do that.
And of course you'll have to change all your existing NSLog()
calls to use [self logIt:]
instead.
This also works on a real device (but the file location is different of course).