// // CLDataPlist.m // SolarBT // // Created by weclouds on 2019/4/24. // Copyright © 2019 weclouds. All rights reserved. // #import "CLDataPlist.h" static CLDataPlist *_plistInfo; @implementation CLDataPlist +(id)sharePlist{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ if (!_plistInfo) { _plistInfo = [[CLDataPlist alloc ]init ]; } }); return _plistInfo; } -(void)writeToPlistWithBlename:(NSString *)blename{ //将用户信息保存在可变数组里边 NSMutableArray *dataArr= [[NSMutableArray alloc]initWithObjects:blename, nil]; [self createDataPlist:dataArr key:blename]; } //创建plist写入操作 //在保存之前遍历一下plist,如果存在就不进行写入操作,这样可以避免保存相同的数据 /** @param dataArr 用户数组 @param key 关键字,这里用用户名来做关键字 */ -(void)createDataPlist:(NSMutableArray *)dataArr key:(NSString *)key{ BOOL isOrSave = YES; //沙盒路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES ); NSString *documentDicectory = [paths objectAtIndex:0]; //plist路径 NSString *plistPath = [documentDicectory stringByAppendingPathComponent:@"userInfo.plist"]; NSFileManager *fileManager = [[NSFileManager alloc]init]; NSMutableDictionary *userInfoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; if ([userInfoDict count] > 1) { //遍历key 如果该用户存在就不进行添加操作 //否则,就进行添加操作 for (id obj in [userInfoDict allKeys]) { NSString *objString = obj; if ([[dataArr objectAtIndex:0] isEqualToString:objString]) { //如果存在,isOrSave设置为0 isOrSave =NO; NSLog(@"该用户已存在"); break; } } } if (isOrSave == YES) { //下边这几个步骤 通过文件管理器,来不判断plist 是否存在,如果不存在, 我们就通过[fileManager createFile:plistPath contents:nil attributes:nil]穿件一个plist 并检测文件是否创建成功 ,如果存在plist 我们就在userInfoDict 可变字典里边保存数据 这样可以避免数据被覆盖 if (![fileManager fileExistsAtPath:plistPath]) { if (![fileManager createFileAtPath:plistPath contents:nil attributes:nil]) { NSLog(@"create file error - 创建文件失败"); } else { NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:dataArr,key, nil]; [userDict writeToFile:plistPath atomically:YES]; } } else { NSMutableDictionary *userInfodictionary =[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]; [userInfodictionary setObject:dataArr forKey:key]; [userInfodictionary writeToFile:plistPath atomically:YES]; } } else { NSMutableDictionary *peripheralDcit = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; [peripheralDcit setObject:dataArr forKey:key]; [peripheralDcit writeToFile:plistPath atomically:YES]; } } -(NSMutableDictionary *)readDataFromPlist{ //沙盒路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES ); NSString *documentDicectory = [paths objectAtIndex:0]; //plist路径 NSString *plistPath = [documentDicectory stringByAppendingPathComponent:@"userInfo.plist"]; NSLog(@"%@",plistPath); NSFileManager *fileManager = [NSFileManager defaultManager]; //更改到操作目录 [fileManager changeCurrentDirectoryPath:[documentDicectory stringByExpandingTildeInPath]]; NSMutableDictionary *userInfoDict = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]; NSLog(@"保存的数据---%@",userInfoDict); return userInfoDict; } -(void)deleleteDatePlist{ //创建文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *document = [paths objectAtIndex:0]; //更改到操作目录 [fileManager changeCurrentDirectoryPath:[document stringByExpandingTildeInPath]]; NSString *plistPath = [document stringByAppendingPathComponent:@"userInfo.plist"]; //如果文件路径出在的话 BOOL bRet = [fileManager fileExistsAtPath:plistPath]; if (bRet) { NSLog(@"清空了plist"); NSError *error ; [fileManager removeItemAtPath:plistPath error:&error]; } } -(void)removeDataWithKey:(NSString *)key{ //创建文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *document = [paths objectAtIndex:0]; //更改到操作目录 [fileManager changeCurrentDirectoryPath:[document stringByExpandingTildeInPath]]; NSString *plistPath = [document stringByAppendingPathComponent:@"userInfo.plist"]; //读取全部内容 NSMutableDictionary *userInfo =[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]; //从字典里边删除对应的key [userInfo removeObjectForKey:key]; //删除后重新写入 [userInfo writeToFile:plistPath atomically:YES]; } @end