CLDataPlist.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // CLDataPlist.m
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/4/24.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. #import "CLDataPlist.h"
  9. static CLDataPlist *_plistInfo;
  10. @implementation CLDataPlist
  11. +(id)sharePlist{
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. if (!_plistInfo) {
  15. _plistInfo = [[CLDataPlist alloc ]init ];
  16. }
  17. });
  18. return _plistInfo;
  19. }
  20. -(void)writeToPlistWithBlename:(NSString *)blename{
  21. //将用户信息保存在可变数组里边
  22. NSMutableArray *dataArr= [[NSMutableArray alloc]initWithObjects:blename, nil];
  23. [self createDataPlist:dataArr key:blename];
  24. }
  25. //创建plist写入操作
  26. //在保存之前遍历一下plist,如果存在就不进行写入操作,这样可以避免保存相同的数据
  27. /**
  28. @param dataArr 用户数组
  29. @param key 关键字,这里用用户名来做关键字
  30. */
  31. -(void)createDataPlist:(NSMutableArray *)dataArr key:(NSString *)key{
  32. BOOL isOrSave = YES;
  33. //沙盒路径
  34. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES );
  35. NSString *documentDicectory = [paths objectAtIndex:0];
  36. //plist路径
  37. NSString *plistPath = [documentDicectory stringByAppendingPathComponent:@"userInfo.plist"];
  38. NSFileManager *fileManager = [[NSFileManager alloc]init];
  39. NSMutableDictionary *userInfoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
  40. if ([userInfoDict count] > 1) {
  41. //遍历key 如果该用户存在就不进行添加操作
  42. //否则,就进行添加操作
  43. for (id obj in [userInfoDict allKeys]) {
  44. NSString *objString = obj;
  45. if ([[dataArr objectAtIndex:0] isEqualToString:objString]) {
  46. //如果存在,isOrSave设置为0
  47. isOrSave =NO;
  48. NSLog(@"该用户已存在");
  49. break;
  50. }
  51. }
  52. }
  53. if (isOrSave == YES) {
  54. //下边这几个步骤 通过文件管理器,来不判断plist 是否存在,如果不存在, 我们就通过[fileManager createFile:plistPath contents:nil attributes:nil]穿件一个plist 并检测文件是否创建成功 ,如果存在plist 我们就在userInfoDict 可变字典里边保存数据 这样可以避免数据被覆盖
  55. if (![fileManager fileExistsAtPath:plistPath]) {
  56. if (![fileManager createFileAtPath:plistPath contents:nil attributes:nil]) {
  57. NSLog(@"create file error - 创建文件失败");
  58. } else {
  59. NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:dataArr,key, nil];
  60. [userDict writeToFile:plistPath atomically:YES];
  61. }
  62. } else {
  63. NSMutableDictionary *userInfodictionary =[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
  64. [userInfodictionary setObject:dataArr forKey:key];
  65. [userInfodictionary writeToFile:plistPath atomically:YES];
  66. }
  67. } else {
  68. NSMutableDictionary *peripheralDcit = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  69. [peripheralDcit setObject:dataArr forKey:key];
  70. [peripheralDcit writeToFile:plistPath atomically:YES];
  71. }
  72. }
  73. -(NSMutableDictionary *)readDataFromPlist{
  74. //沙盒路径
  75. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES );
  76. NSString *documentDicectory = [paths objectAtIndex:0];
  77. //plist路径
  78. NSString *plistPath = [documentDicectory stringByAppendingPathComponent:@"userInfo.plist"];
  79. NSLog(@"%@",plistPath);
  80. NSFileManager *fileManager = [NSFileManager defaultManager];
  81. //更改到操作目录
  82. [fileManager changeCurrentDirectoryPath:[documentDicectory stringByExpandingTildeInPath]];
  83. NSMutableDictionary *userInfoDict = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
  84. NSLog(@"保存的数据---%@",userInfoDict);
  85. return userInfoDict;
  86. }
  87. -(void)deleleteDatePlist{
  88. //创建文件管理器
  89. NSFileManager *fileManager = [NSFileManager defaultManager];
  90. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  91. NSString *document = [paths objectAtIndex:0];
  92. //更改到操作目录
  93. [fileManager changeCurrentDirectoryPath:[document stringByExpandingTildeInPath]];
  94. NSString *plistPath = [document stringByAppendingPathComponent:@"userInfo.plist"];
  95. //如果文件路径出在的话
  96. BOOL bRet = [fileManager fileExistsAtPath:plistPath];
  97. if (bRet) {
  98. NSLog(@"清空了plist");
  99. NSError *error ;
  100. [fileManager removeItemAtPath:plistPath error:&error];
  101. }
  102. }
  103. -(void)removeDataWithKey:(NSString *)key{
  104. //创建文件管理器
  105. NSFileManager *fileManager = [NSFileManager defaultManager];
  106. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  107. NSString *document = [paths objectAtIndex:0];
  108. //更改到操作目录
  109. [fileManager changeCurrentDirectoryPath:[document stringByExpandingTildeInPath]];
  110. NSString *plistPath = [document stringByAppendingPathComponent:@"userInfo.plist"];
  111. //读取全部内容
  112. NSMutableDictionary *userInfo =[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
  113. //从字典里边删除对应的key
  114. [userInfo removeObjectForKey:key];
  115. //删除后重新写入
  116. [userInfo writeToFile:plistPath atomically:YES];
  117. }
  118. @end