// 连接错误报错的方法 - (void)getErrorMethod:(NSError *)error{ // 获取当前时间 NSDateFormatter* formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"]; NSString* date = [formatter stringFromDate:[NSDate date]]; // 获取错误的所在类 NSString *errorClass = NSStringFromClass([self class]); // 获取错误所在的方法 NSString *errorMethod = NSStringFromSelector(_cmd); // 获取错误所在的行数 NSString *lineNumber = [NSString stringWithFormat:@"%d",__LINE__]; NSString *excpCnt = [NSString stringWithFormat: @"date: %@ errorClass: %@ errorMethod: %@ lineNumber: %@ ", date,errorClass,errorMethod,lineNumber]; [self saveErrorLog:excpCnt]; } // 保存日志的方法 - (void)saveErrorLog:(NSString *)excpCnt{ NSArray *dirArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *dirPath = dirArr[0]; NSString *logDir = [dirPath stringByAppendingString:@"/CrashLog"]; BOOL isExistLogDir = YES; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:logDir]) { isExistLogDir = [fileManager createDirectoryAtPath:logDir withIntermediateDirectories:YES attributes:nil error:nil]; } if (isExistLogDir) { // 此处可扩展 NSString *logPath = [logDir stringByAppendingString:@"/crashLog.txt"]; // 没有log.txt文件就新建一个 if (![fileManager fileExistsAtPath:logPath]) { [excpCnt writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; } // 有的话就追加文字 else{ NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:logPath]; [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾 NSData* stringData = [excpCnt dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:stringData]; //追加写入数据 [fileHandle closeFile]; } } }