#pragma mark - 自定义方法,将nslog的输出信息写入到dr.log文件中
- (void)nslogToFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*documentDirectory = [pathsobjectAtIndex:0];
NSString*fileName = [NSStringstringWithFormat:@"NSLog.txt"];// 注意不是NSData!
NSString*logFilePath = [documentDirectorystringByAppendingPathComponent:fileName];
// 先删除已经存在的文件
NSFileManager *defaultManager = [NSFileManager defaultManager];
[defaultManagerremoveItemAtPath:logFilePatherror:nil];
// 将log输入到文件
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
在application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions中调用;
- (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
[self nslogToFile];
return YES;
}