// 获得应用程序沙盒的Documents文件夹路径
NSString *documentPaths=NSSearchPathForDirectoriesInDomains[(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
//输出沙盒Documents文件夹路径
NSLog(@"Documents path: %@",documentPath);
// 获得应用程序沙盒的Caches文件夹路径
NSString *CachesPath=NSSearchPathForDirectoriesInDomains[(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
//输出沙盒
Caches
文件夹路径
NSLog(@"Caches path: %@",CachesPath);
// 获得应用程序沙盒的Downloads文件夹路径
NSString *loadPathsPath=NSSearchPathForDirectoriesInDomains[(NSDownloadsDirectory,NSUserDomainMask,YES) objectAtIndex:0];
//输出沙盒
Downloads
文件夹路径
NSLog(@"Downloads path: %@",loadPathsPath);
//第二种获取沙盒下路径的方式
// 获得应用程序沙盒的home文件夹路径
NSString *homePath= NSHomeDirectory();
// 获得应用程序沙盒的tmp文件夹路径
NSString *TmpPath= NSTemporaryDirectory();
// 将文件写入到指定程序沙盒的指定文件夹中如Documents
NSString *fileName=[homePath stringByAppendingPathComponent:@"Documents"];
NSString *content=@"a";
NSData *contentData=[content dataUsingEncoding:NSUTF8StringEncoding];
//此处只能写入基本类型的东西
if ([contentData writeToFile:fileName atomically:YES]) {
NSLog(@"写入成功");
}