ChatCacheFileUtil.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // ChatCacheFileUtil.m
  3. // NewMC
  4. //
  5. // Created by 话语科技 on 12-10-25.
  6. //
  7. //
  8. #import "ChatCacheFileUtil.h"
  9. NSString *const myUSERID=@"myUSERID";
  10. @implementation ChatCacheFileUtil
  11. static ChatCacheFileUtil *sharedInstance;
  12. + (ChatCacheFileUtil*)sharedInstance
  13. {
  14. if (sharedInstance==nil) {
  15. sharedInstance = [[ChatCacheFileUtil alloc] init];
  16. }
  17. return sharedInstance;
  18. }
  19. - (id)init
  20. {
  21. return [super init];
  22. }
  23. - (NSString*)userDocPath{
  24. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  25. NSString *userFolderPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/",MY_USER_ID]];
  26. NSFileManager *fileManager = [NSFileManager defaultManager];
  27. if (![fileManager fileExistsAtPath:userFolderPath]) {
  28. [fileManager createDirectoryAtPath:userFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
  29. }
  30. return userFolderPath;
  31. }
  32. - (BOOL) deleteWithContentPath:(NSString *)thePath{
  33. NSError *error=nil;
  34. NSFileManager *fileManager = [NSFileManager defaultManager];
  35. if ([fileManager fileExistsAtPath:thePath]) {
  36. [fileManager removeItemAtPath:thePath error:&error];
  37. }
  38. if (error) {
  39. NSLog(@"删除文件时出现问题:%@",[error localizedDescription]);
  40. return NO;
  41. }
  42. return YES;
  43. }
  44. - (NSString*)chatCachePathWithFriendId:(NSString*)theFriendId andType:(NSInteger)theType
  45. {
  46. NSString *userChatFolderPath = [[self userDocPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"chatLog/%@/",theFriendId]];
  47. switch (theType) {
  48. case 1:
  49. userChatFolderPath = [userChatFolderPath stringByAppendingPathComponent:@"voice/"];
  50. break;
  51. case 2:
  52. userChatFolderPath = [userChatFolderPath stringByAppendingPathComponent:@"image/"];
  53. break;
  54. default:
  55. break;
  56. }
  57. NSFileManager *fileManager = [NSFileManager defaultManager];
  58. if (![fileManager fileExistsAtPath:userChatFolderPath]) {
  59. [fileManager createDirectoryAtPath:userChatFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
  60. }
  61. return userChatFolderPath;
  62. }
  63. - (void)deleteFriendChatCacheWithFriendId:(NSString*)theFriendId
  64. {
  65. NSString *userChatFolderPath = [[self userDocPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"chatLog/%@/",theFriendId]];
  66. [[NSFileManager defaultManager] removeItemAtPath:userChatFolderPath error:nil];
  67. }
  68. - (void)deleteAllFriendChatDoc
  69. {
  70. NSString *userChatFolderPath = [[self userDocPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"chatLog/"]];
  71. [[NSFileManager defaultManager] removeItemAtPath:userChatFolderPath error:nil];
  72. }
  73. @end