Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

cocoa touch - Is there a way to capture the output of NSLog on an iPhone when not connected to a debugger?

I'm logging a bunch of data with NSLog(). Is there a way to capture the log data when my iPhone is not connected to my development machine and running under a debugger?

For example, can I redirect it to a file and then read the log file back through Xcode at a later point in time? I need to do this in order to test my app when the WiFi is poor, which necessitates that I go far away from my desk.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The method below will create a file name “console.log” in the Documents folder of your application so you can later read it.

Call this method at the beginning of your program:

- (void) redirectConsoleLogToDocumentFolder
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
  freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}

The log will never be erased, so use with caution.

Once you have tested your app in the field, reconnect your phone to your Mac, in Xcode, open the Organizer. In the Summary panel, you have the list of all the apps on your phone. Expand the one you're debugging, and you'll see a package named "Application Data".

Click the arrow on the right of its name and save this. You'll end with a folder with a name of your Bundle Identifier followed by a date.

Inside this folder you'll find your Documents Folder, which should contain the console.log


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...