1.在AppDelegate.m中创建函数实现以下代码块:
-(void)redirectNSlogToDocumentFolder
{
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentDirectory=[pathsobjectAtIndex:0];
NSString*fileName=[NSStringstringWithFormat:@"MrNSLog.txt"];//注意不是NSData!
NSString*logFilePath = [documentDirectorystringByAppendingPathComponent:fileName];
//先删除已经存在的文件
NSFileManager*defaultManager = [NSFileManagerdefaultManager];
[defaultManagerremoveItemAtPath:logFilePatherror:nil];
//将log输入到文件
freopen([logFilePathcStringUsingEncoding:NSASCIIStringEncoding],"a+",stdout);
freopen([logFilePathcStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
2.在didFinishLaunchingWithOptions中调用 :
[selfredirectNSlogToDocumentFolder];