iOS

AFN 3.0相关

学习笔记

Posted by Alpaca on July 14, 2016

下载

@property (nonatomic,strong) NSURLSessionDownloadTask *downloadTask;

   
{ 
   // 下载图片
NSURL *URL = @"abc.png";

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

//AFN3.0+基于封住URLSession的句柄
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

//请求
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

//下载Task操作
_downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
    
    // @property int64_t totalUnitCount;  需要下载文件的总大小
    // @property int64_t completedUnitCount; 当前已经下载的大小
    
    // 给Progress添加监听 KVO
    NSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
    // 回到主队列刷新UI
    dispatch_async(dispatch_get_main_queue(), ^{
        self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
    });
    
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
    
    //- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径
    
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
    return [NSURL fileURLWithPath:path];
    
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
    // filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用
    
    NSString *imgFilePath = [filePath path];// 将NSURL转成NSString
    NSLog(@"%@",imgFilePath);
 }    

缓存清理

删除沙盒里的具体文件

-(void)deleteFile {
	NSFileManager* fileManager=[NSFileManager defaultManager];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//文件名
//    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"demo2.js"];
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"8385.jpeg"];

BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (!blHave) {
    NSLog(@"no  have");
    return ;
}else {
    NSLog(@" have");
    BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
    if (blDele) {
        NSLog(@"dele success");
    }else {
        NSLog(@"dele fail");
    	}
	}     
}

删除沙盒某个路径所有文件(清空缓存)

-(void)deleteAllFile {
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) lastObject ];

NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];

NSLog ( @"cachpath = %@" , cachPath);

for ( NSString * p in files) {
    
    NSError * error = nil ;
    
    NSString * path = [cachPath stringByAppendingPathComponent :p];
    
    if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
        
     BOOL blDele = [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
        if (blDele) {
            NSLog(@"delet success");
        }else {
            NSLog(@"delet fail");
        	}
    	}  
	}
}

文件路径

-(void)findPath{

// 本地:
//    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];

// 读取沙盒
NSString *_cachePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *sourcePath =  [_cachePath stringByAppendingPathComponent:@"demo2.js"];


// NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];

}


分享文章到微博:
对您有点帮助? 您的支持将鼓励我继续创作!