沙盒,plist,NSUser
2016-04-04 00:08
375 查看
沙盒路径,NSBund的使用,plist文件的使用
//第一种方法:获取本地documents路径,library路径,temp路径
NSString *documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
NSString *libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
NSString *tempPath=NSTemporaryDirectory();
//第二种方法:
NSString *DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents"];
NSString *LibraryPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Library"];
NSString *tmpPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/tmp"];
//保存一张图片到documents下的ios文件夹中
NSString *imageDir = [NSString
stringWithFormat:@"%@/documents/%@",
NSHomeDirectory(), @"ios"];
NSFileManager *fileManager = [NSFileManager
defaultManager];
if([fileManager
fileExistsAtPath:imageDir]){
NSLog(@"存在");
}else{
NSLog(@"不存在");
//创建文件夹路径
[[NSFileManager
defaultManager] createDirectoryAtPath:imageDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
UIImage *image = [UIImage
imageNamed:@"1.png"];//设置一个图片的存储路径
NSString *imagePath = [imageDir
stringByAppendingString:@"/aa.png"];//设置一个图片的存储路径
[UIImagePNGRepresentation(image)
writeToFile:imagePath atomically:YES];//写入到ios文件夹中
//保存 .txt 文件到documents下的ios文件夹中
NSString *imageDir = [NSString
stringWithFormat:@"%@/documents/%@",
NSHomeDirectory(), @"ios"];
NSFileManager *fileManager = [NSFileManager
defaultManager];
if([fileManager
fileExistsAtPath:imageDir]){
NSLog(@"存在");
}else{
NSLog(@"不存在");
//创建文件夹路径
[[NSFileManager
defaultManager] createDirectoryAtPath:imageDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
NSDictionary *dic =
@{@"content":@"我是测试的",@"name":@"林毛家"};
NSString *filePath = [imageDir
stringByAppendingPathComponent:@"aa.txt"];
NSLog(@"%@",filePath);
if ([dic
writeToFile:filePath atomically:YES]) {
NSLog(@"保存成功");
}else{
NSLog(@"保存失败");
}
//从Documents读取图片图片
NSString *imgPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents/ios/aa.png"];
UIImage *img = [UIImage
imageWithContentsOfFile:imgPath];
//遍历Documents下的所有文件名称
NSFileManager *fileManager = [NSFileManager
defaultManager];
NSString *DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents"];//获取本地Documents路径
NSError *error =
nil;
NSArray *fileList = [[NSArray
alloc] init];
fileList=[fileManager contentsOfDirectoryAtPath:DocumentsPath
error:&error];
NSLog(@"%@",fileList);
//取出plist文件中的数据
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"phone"
ofType:@"plist"];
NSArray *array = [NSArray
arrayWithContentsOfFile:plistPath];
NSMutableArray* dataSource = [NSMutableArray
arrayWithArray:array];
NSLog(@"%@",dataSource);
//NSUserDefaults
的使用
NSUserDefaults *UserDefaults=[NSUserDefaults
standardUserDefaults];
//保存用户头像
UIImage *image=[UIImage
imageNamed:@"aa.png"];
[UserDefaults setObject:UIImagePNGRepresentation(image)
forKey:@"image"];
UIImage *Newimage=[UIImage
imageWithData:[UserDefaults
objectForKey:@"image"]];
//保存用户名
[UserDefaults setObject:@"林毛家"
forKey:@"name"];
NSString *name=[UserDefaults
objectForKey:@"name"];
//判断是否第一次登陆
[UserDefaults setBool:YES
forKey:@"isFirst"];
//从bundel获取文件路径
NSString *theupfilePath=[[NSBundle
mainBundle]pathForResource:@"aa"ofType:@"gif"];
//第一种方法:获取本地documents路径,library路径,temp路径
NSString *documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
NSString *libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
NSString *tempPath=NSTemporaryDirectory();
//第二种方法:
NSString *DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents"];
NSString *LibraryPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Library"];
NSString *tmpPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/tmp"];
//保存一张图片到documents下的ios文件夹中
NSString *imageDir = [NSString
stringWithFormat:@"%@/documents/%@",
NSHomeDirectory(), @"ios"];
NSFileManager *fileManager = [NSFileManager
defaultManager];
if([fileManager
fileExistsAtPath:imageDir]){
NSLog(@"存在");
}else{
NSLog(@"不存在");
//创建文件夹路径
[[NSFileManager
defaultManager] createDirectoryAtPath:imageDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
UIImage *image = [UIImage
imageNamed:@"1.png"];//设置一个图片的存储路径
NSString *imagePath = [imageDir
stringByAppendingString:@"/aa.png"];//设置一个图片的存储路径
[UIImagePNGRepresentation(image)
writeToFile:imagePath atomically:YES];//写入到ios文件夹中
//保存 .txt 文件到documents下的ios文件夹中
NSString *imageDir = [NSString
stringWithFormat:@"%@/documents/%@",
NSHomeDirectory(), @"ios"];
NSFileManager *fileManager = [NSFileManager
defaultManager];
if([fileManager
fileExistsAtPath:imageDir]){
NSLog(@"存在");
}else{
NSLog(@"不存在");
//创建文件夹路径
[[NSFileManager
defaultManager] createDirectoryAtPath:imageDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
NSDictionary *dic =
@{@"content":@"我是测试的",@"name":@"林毛家"};
NSString *filePath = [imageDir
stringByAppendingPathComponent:@"aa.txt"];
NSLog(@"%@",filePath);
if ([dic
writeToFile:filePath atomically:YES]) {
NSLog(@"保存成功");
}else{
NSLog(@"保存失败");
}
//从Documents读取图片图片
NSString *imgPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents/ios/aa.png"];
UIImage *img = [UIImage
imageWithContentsOfFile:imgPath];
//遍历Documents下的所有文件名称
NSFileManager *fileManager = [NSFileManager
defaultManager];
NSString *DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents"];//获取本地Documents路径
NSError *error =
nil;
NSArray *fileList = [[NSArray
alloc] init];
fileList=[fileManager contentsOfDirectoryAtPath:DocumentsPath
error:&error];
NSLog(@"%@",fileList);
//取出plist文件中的数据
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"phone"
ofType:@"plist"];
NSArray *array = [NSArray
arrayWithContentsOfFile:plistPath];
NSMutableArray* dataSource = [NSMutableArray
arrayWithArray:array];
NSLog(@"%@",dataSource);
//NSUserDefaults
的使用
NSUserDefaults *UserDefaults=[NSUserDefaults
standardUserDefaults];
//保存用户头像
UIImage *image=[UIImage
imageNamed:@"aa.png"];
[UserDefaults setObject:UIImagePNGRepresentation(image)
forKey:@"image"];
UIImage *Newimage=[UIImage
imageWithData:[UserDefaults
objectForKey:@"image"]];
//保存用户名
[UserDefaults setObject:@"林毛家"
forKey:@"name"];
NSString *name=[UserDefaults
objectForKey:@"name"];
//判断是否第一次登陆
[UserDefaults setBool:YES
forKey:@"isFirst"];
//从bundel获取文件路径
NSString *theupfilePath=[[NSBundle
mainBundle]pathForResource:@"aa"ofType:@"gif"];
相关文章推荐
- 【一周读书】和阅读的500天
- Spring官网下载dist.zip的几种方法
- 请描述一下 cookies,sessionStorage 和 localStorage 的区别?
- MFC框架仿真<三>R T T I
- bzoj1033 杀蚂蚁
- 响应者链
- 第五周作业
- [渗透测试]XML注入
- Muduo网络库源码分析(一) EventLoop事件循环(Poller和Channel)
- 本地提交到Bitbucket
- python操作redis
- Java第五周学习总结
- Dockerfile创建Linux/CentOS6.x Nginx MySQL Tomcat PHP(LNMTP)镜像 推荐
- 大话设计模式—责任链模式
- iOS QQ实现第三方登
- 详解卷积神经网络(CNN)
- PHP版本的选择5.2.17 5.3.27 5.3.28 5.4 5.5兼容性问题分析
- 在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的运行环境
- php文件上传的两种实现方法
- php自动加载方式集合