IOS:OC-沙盒处理

作者: 任任任任师艳 | 来源:发表于2017-06-03 08:03 被阅读0次

1.新建类 “sandBox”
在sandBox.h中
//写接口

pragma mark --- (封装沙河路径方法)

//获取document文件目录
+(NSString *)getDocumentDirectory;

//获取library目录
+(NSString *)getLibraryDirectionary;
//获取library/Caches目录
+(NSString *)getCachesDirectionary;
//获取library/Preferences
+(NSString *)getPreferencesDirectory;
//获取temp文件路径
+(NSString *)getTempDirectory;
//获取xxxx.app文件路径
+(NSString *)getAppPath;
在sandBox.m中

pragma mark --- (封装沙河路径方法)

//获取document文件目录
//将程序当中建立的或者在程序中浏览的数据保存在这
+(NSString *)getDocumentDirectory{

/*
 三个参数:
 第一个参数:枚举(具体要查看的某个文件目录【进入的文件夹】)
 第二个参数: NSUserDomainMask用户主目录
 第三个参数:YES 设置为表示展开完整路径
 
 NSSearchPathForDirectoriesInDomains代表查找沙盒路径,返回值是一个数组,该书中当中只有一个元素,这个元素就是路径
 */

NSString * documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
return documentPath;

}

//获取library目录
//保存的是程序默认设置,或者一些状态信息
+(NSString *)getLibraryDirectionary{

  return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];

}
//获取library/Caches目录
//存放地是缓存文件。。音频视频
+(NSString *)getCachesDirectionary{

return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];

}
//获取library/Preferences
//偏好设置文件app 偏好设置存储在这里,例如:是否是否访问图片,是否访问地理位置
+(NSString *)getPreferencesDirectory{

//获取到library/Preferences目录

NSArray *path2= NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
//是将前面路径格式和后面的字符串给拼接在一起
//并且已路径格式返回
NSString * path1=[[path2 objectAtIndex:0]stringByAppendingString:@"Preference"];
return path1;
//return [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES)];

}
//获取temp文件路径
+(NSString *)getTempDirectory{

return NSTemporaryDirectory();

}
//获取xxxx.app文件路径
+(NSString *)getAppPath{

return [[NSBundle mainBundle]resourcePath];

}

  1. ViewController.m
  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    #warning --- 简单对象的读写:
    //简单对象:《字符串/数组/字典/数据(data)》
    [self writefile];
    [self myFileManager];

}

pragma mark--- 文件管理 ---创建---

-(void)myFileManager{
//初始化NSFileManager
//defaultManager方法为单例方法
NSFileManager * manager = [NSFileManager defaultManager];
//获取跟路径
NSString * path = NSHomeDirectory();

path = [path stringByAppendingPathComponent:@"text/myApp"];

//创建目录
[manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

//沙盒中创建文件

// NSString * path2=[path stringByAppendingString:@"text2/App"];
//
// [manager createDirectoryAtPath:path2 withIntermediateDirectories:YES attributes:nil error:nil];
// NSLog(@"%@",path);

pragma mark ---文件管理 ---添加:

path =[path stringByAppendingPathComponent:@"fuchuan.txt"];

// NSLog(@"%@",path);
//写入内容
NSString * string = @"我是向日葵";
//将文本写入txt中
BOOL result=[string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (result){
NSLog(@"写入成功%@",path);
}
else{
NSLog(@"失败");
}
}

warning --- 简单对象的读写:

-(void)writefile{
/*
//字符串写入
//首先获取document文件路径
NSArray * documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取首元素
NSString * documentpath=documentArray[0];
//文件夹名字<字符串.txt>
documentpath=[documentpath stringByAppendingString:@"/zifuchuan.txt"];

//NSLog(@"%@",documentpath);
//文本内容
NSString *string =@"我有一百种方法让你爱上我,其中一种就是可劲调戏你";
//将文本内容写入的doucument的文件当中
BOOL result =[string writeToFile:documentpath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(result){
    NSLog(@"写入成功,数据就是%@",documentpath);
}
else{
    NSLog(@"数据写入失败");
}
*/
/*
//数组写入到沙盒文件中

//获取文件路径
NSString * document=[sandBox getDocumentDirectory];

//在document下拼接文件名;
NSString * path =[document stringByAppendingString:@"array.plist"];
//创建数组
NSArray * arrayFile = @[@"华晨宇",@"吴亦凡",@"宋仲基",@"向日葵"];
//将数组写入到documents文件当中
[arrayFile writeToFile:path atomically:YES];
NSLog(@"****%@",path);
 */
/*
//将字典写入到沙盒文件中
//写字典
//获取document路径
NSString * dicPath = [sandBox getDocumentDirectory];
//在document下拼接文件名;
NSString * path=[dicPath stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary * dict=@{@"name":@"小花"};
 //将字典写入到documents文件当中
[dict writeToFile:path atomically:YES];
NSLog(@"%@",path);
 */

//将数据写入到沙盒文件中
//获取路径并创建文件
NSString * dataPath=[[sandBox getDocumentDirectory]stringByAppendingPathComponent:@"data.a"];
//
NSString *datastr = @"阿尔卑斯";
//将string转化为NSData类型
NSData * mydata= [datastr dataUsingEncoding:NSUTF8StringEncoding];
//写入到文件中
[mydata writeToFile:dataPath atomically:YES];
NSLog(@"")

}

相关文章

  • IOS:OC-沙盒处理

    1.新建类 “sandBox”在sandBox.h中//写接口 pragma mark --- (封装沙河路径方...

  • iOS 系统相关复习

    沙盒 iOS沙盒详细介绍iOS沙盒篇 沙盒机制介绍 iOS中的沙盒机制是一种安全体系。为了保证系统安全,iOS每个...

  • iOS 数据持久化知识汇总(1)—————存储路径

    一、沙盒和沙盒存储路径 1、沙盒是什么 iOS 每个iOS应用都有自己的应用沙盒,应用沙盒就是文件系统目录 。所...

  • 05-iOS数据存储

    一、iOS沙盒机制 iOS的每个应用都有属于自己的存储空间,即沙盒应用只能访问自己的沙盒,不可访问其他区域。 沙盒...

  • 使用沙盒的正确姿势

    在学习iOS存储方法之前,先了解一下iOS存储机制——沙盒应用沙盒机制:每个iOS应用都有自己的应用沙盒(文件系统...

  • Objective-C沙盒结构

    导读: 一、什么是沙盒机制二、沙盒的特点三、沙盒的结构组成四、获取沙盒目录路径 一、什么是沙盒机制 iOS中的沙盒...

  • iOS 沙盒

    沙盒机制:在iOS中每个APP都拥有自己的沙盒,APP只能访问对应沙盒中存储的数据, iOS是不允许跨越沙盒去访问...

  • OC - 沙盒

    导读: 一、什么是沙盒机制 二、沙盒的特点 三、沙盒的结构组成 四、获取沙盒目录路径 一、什么是沙盒机制 iOS中...

  • 沙盒

    一、iOS沙盒机制介绍(1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 ...

  • iOS—数据处理之文件读写

    数据处理之文件读写 沙盒机制(SandBox) 沙盒:每一个iOS应用都会为自己创建一个文件系统目录(文件夹),这...

网友评论

    本文标题:IOS:OC-沙盒处理

    本文链接:https://www.haomeiwen.com/subject/jbypfxtx.html