沙盒管理

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

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

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

//获取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
  • viewDidLoad {[super viewDidLoad];// Do any additional setup after
    loading the view, typically from a nib.#warning —
    简单对象的读写://简单对象:《字符串/数组/字典/数据》[self
    writefile];[self myFileManager];

}

-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);

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

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

-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{ 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

}

相关文章