沙盒

一、沙盒有几个目录:

xCode6之前有4个:Documents,Lib,temp,app (两年前)

xCode6有3个:Documents,Lib,tmp

即修改了app的资源目录

 

二、

最多操作的两个文件:Documents、Caches

Lib下有两个文件:配置文件目录Preferences+缓存目录Caches

 

三、常用目录

1.家目录(项目的根目录)

获取家目录:NSHomeDirectory(); 

真机在家目录下是没有读写权限的,不要进行读写操作;

要进行文件操作,可以在Documents和Lib下的Caches下操作

2.Documents

获取Documents目录:

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO)[0];

NSURL *url = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];

 

NSLog(@"%@",[url relativePath]);

 

 

3.Cache

获取Cache目录:NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

4.临时目录

保存比较大的临时数据(如下载`NSURLSessionDownloadTask`),下载完后会自动清除在这个目录下的文件,可在这之前进行文件操作(如另存到别的目录下),

获取临时目录:NSTemporaryDirectory();

 

  获取常用目录:

 1    //1.获取家目录(真机没有读写权限)
 2     NSHomeDirectory();
 3     //2.获取documents路径
 4     /*
 5      参数1:目录
 6      参数2:NSUserDomainMask用户当前用户安装目录(沙盒)
 7      参数3:是否显示所有完整路径(YES表示显示,NO表示:~/xxxx)
 8      */
 9     NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO)[0];
10     NSLog(@"%@",documents);
11     //3.Cache目录
12     NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
13     NSLog(@"%@",cache);
14     //4.获取临时目录:
15     NSTemporaryDirectory();

  

  临时目录应用实例 ---- 下载文件NSURLSessionDownloadTask:

  @interface ViewController ()<NSURLSessionDownloadDelegate>

   @end

- (void)download
{
    //配置session
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    //创建含配置的session请求对象,同时设置代理和线程
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    //session.delegate是readonly
    
    //下载的请求,需要走代理方法 ---> 不能用单例创建session
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3"]];
    
    //发送请求
    [downloadTask resume];
}
//设置保存目录
- (NSString *)toPath
{
    //获取documents路径
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    //documents/test.mp3
    return [documentPath stringByAppendingPathComponent:@"test.mp3"];
}

#pragma mark - NSURLSessionDownloadTaskDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
    //当前下载保存的临时目录  relativePath:相对路径    description:本地路径
    NSString *path = [location relativePath];
    
    NSLog(@"下载完成:%@",path);
    /*
     path:/Users/Administrator/Library/Developer/CoreSimulator/Devices/8B2B3965-EB5D-41CF-BCD2-9D669B8E8FF4/data/Containers/Data/Application/88B2E4DA-7759-4539-A3E6-8409F2356C24/tmp/CFNetworkDownload_qxfNCy.tmp
     
     path的组成:临时目录/临时文件名.tmp
     前往该path,发现找不到路径
     
     原因:下载时,临时保存文件到这个路径,文件名随机生成,如xxx.tmp,下载结束后自动删除该文件
     
     若要保存临时文件,需要将文件移动到documents路径下保存。
     */
    
    //移动到哪个目录
    NSString *toPath = [self toPath];
    
    //把当前下载文件移动到目标目录
    [[NSFileManager defaultManager] moveItemAtPath:path toPath:toPath error:nil];
}

/**
 *  下载进度回调
 *
 *  @param session                   session
 *  @param downloadTask              downloadTask
 *  @param bytesWritten              当前段的字节大小
 *  @param totalBytesWritten         总共下载的字节
 *  @param totalBytesExpectedToWrite 总大小
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    NSLog(@"%0.2f",(float)totalBytesWritten/totalBytesExpectedToWrite);
}

 

posted on 2016-03-18 19:19  Wilson_CYS  阅读(211)  评论(0编辑  收藏  举报

导航