保存日志到手机:
1、将下面的代码加到Appdelegate里,在didFinshLunch中调用
2、手机连接到Mac,在Xcode中Window—》Device —》选中当前设备和app 在下面选择DownLoadContainer
- (void)redirectNSLogToDocumentFolder
{
//如果已经连接Xcode调试则不输出到文件
// if(isatty(STDOUT_FILENO)) {
// return;
// }
UIDevice *device = [UIDevice currentDevice];
if([[device model] hasSuffix:@"Simulator"]){ //在模拟器不保存到文件中
return;
}
//将NSlog打印信息保存到Document目录下的Log文件夹下
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileExists = [fileManager fileExistsAtPath:logDirectory];
if (!fileExists) {
[fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //每次启动后都保存一个新的日志文件中
NSString *dateStr = [formatter stringFromDate:[NSDate date]];
NSString *logFilePath = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr];
// 将log输入到文件
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
//未捕获的Objective-C异常日志
// NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
}