201406041133326.jpg
Download類(lèi)的基本使用
download下載類(lèi)是系統(tǒng)自帶的下載類(lèi)嗽桩,它可以將會(huì)話(huà)對(duì)象封裝成一個(gè)下載任務(wù)睹限,它不需要文件句柄也不需要輸出流就能實(shí)現(xiàn)文件的下載晚伙,但它把文件下載結(jié)束之后會(huì)將文件放在一個(gè)temp臨時(shí)路徑里颖低,而且這個(gè)路徑下的文件會(huì)隨時(shí)被系統(tǒng)刪除,因此陌宿,為了保證文件的安全性锡足,我們需要手動(dòng)將下載好的文件移動(dòng)到安全的路徑下。具體代碼如下:
-(void)download1{
//創(chuàng)建請(qǐng)求路徑
NSURL *url=[NSURL URLWithString:@"http://6:32812/resources/images/minion_01.png"];
//創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//創(chuàng)建會(huì)話(huà)對(duì)象
NSURLSession *session=[NSURLSession sharedSession];
//根據(jù)會(huì)話(huà)對(duì)象來(lái)創(chuàng)建下載任務(wù)
NSURLSessionDownloadTask *downloadTask=[session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"%@",location);
//拼接要拷貝到的目的路徑壳坪,因?yàn)閿?shù)據(jù)默認(rèn)是放在一個(gè)臨時(shí)路徑下面舶得,臨時(shí)路徑不安全,因?yàn)闀?huì)隨時(shí)被刪除
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:response.suggestedFilename];
NSURL *url=[NSURL fileURLWithPath:path];
//移動(dòng)文件
[[NSFileManager defaultManager]moveItemAtURL:location toURL:url error:nil];
NSLog(@"%@",url);
}];
//提交任務(wù)
[downloadTask resume];
}
除了上面那個(gè)方法外爽蝴,我們還可以通過(guò)代理的方法實(shí)現(xiàn)沐批,而且使用代理還可以監(jiān)聽(tīng)文件的下載進(jìn)度:
-(void)download2{
//確定請(qǐng)求路徑
NSURL *url=[NSURL URLWithString:@"http://120.32812/resources/images/minion_01.png"];
//創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//創(chuàng)建會(huì)話(huà)對(duì)象并設(shè)置代理
NSURLSession *session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
//根據(jù)會(huì)話(huà)對(duì)象創(chuàng)建請(qǐng)求任務(wù)
NSURLSessionDownloadTask *downloadTask=[session downloadTaskWithRequest:request];
[downloadTask resume];
}
// bytesWritten :本次寫(xiě)入的數(shù)據(jù)大小 totalBytesWritten:寫(xiě)入的總大小
// totalBytesExpectedToWrite :需要下載的數(shù)據(jù)的總大小
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
//監(jiān)聽(tīng)文件下載進(jìn)度
NSLog(@"%f",1.0*totalBytesWritten/totalBytesExpectedToWrite);
}
//下載完成的時(shí)候調(diào)用
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
//剪切文件到安全的位置
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
NSURL *fullPath=[NSURL fileURLWithPath:path];
[[NSFileManager defaultManager]moveItemAtURL:location toURL:fullPath error:nil];
NSLog(@"%@",fullPath);
}
//請(qǐng)求結(jié)束的時(shí)候調(diào)用(并不是發(fā)生錯(cuò)誤的時(shí)候才調(diào)用)
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
NSLog(@"---------");
}