沙盒里的文件夹包括Documents、Library、tmp。这三个文件夹
Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
首先是将数据存储的沙盒路径下的Documents文件夹下
NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"Dome.plist"]; NSString * str = [NSString stringWithFormat:@"%d",storageID]; NSMutableDictionary *dataDic = [NSMutableDictionary dictionary]; [dataDic setObject:self.accountTX.text forKey:@"userName"]; [dataDic setObject:self.PwdTX.text forKey:@"data"]; [dataDic setObject:str forKey:@"state"]; [dataDic writeToFile:filePatch atomically:YES];
接下来是从Documents路径下取出存储过的数据
NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"Dome.plist"]; //读取plist文件的内容 NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePatch]; self.accountTX.text = [dataDictionary objectForKey:@"userName"]; self.PwdTX.text = [dataDictionary objectForKey:@"data"]; storageID = [[dataDictionary objectForKey:@"state"] intValue];
最后是清空存储的文件
NSFileManager *fileMger = [NSFileManager defaultManager]; NSString *LyPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"Dome.plist"]; //如果文件路径存在的话 BOOL bRet = [fileMger fileExistsAtPath:LyPath];if (bRet) { NSError *err; [fileMger removeItemAtPath:LyPath error:&err]; }
能力有限,只能写这么多.有什么问题欢迎大家留言指正,我看到一定回复.感谢!!!!!!!
网友评论