李洪强iOS开发之下载


//

//

// LHQDownLoader.m

// A21 - 李洪强 - 下载

//

// Created by vic fan on 16/7/3.

// Copyright © 2016 李洪强. All rights reserved.

//

#import "LHQDownLoader.h"

@interface LHQDownLoader ()<NSURLSessionDataDelegate>

@end

@implementation LHQDownLoader

- (void)download: (NSString *)strUrl{

//构造secssion

// NSURLSession *secssion = [NSURLSession sharedSession];

NSURLSessionConfiguration *cfg = [ NSURLSessionConfiguration defaultSessionConfiguration

];

// 1. 得到 session 对象

NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];

//2 创建一个下载task

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSURL *url = [NSURL URLWithString:strUrl];

NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

}];

//3 开始任务

[task resume];

}

//注意:

//NSURLSessionDownloadTask下载完成之后,将数据保存在沙盒里面的tmp临时文件中,需要将临时文件 将临时文件剪切或者复制 Caches 文件夹。

#pragma mark - NSURLSessionDownloadDelegate

/**

下载完成

* @param location 临时文件的路径(下载好的文件)

*/

- ( void )URLSession:( NSURLSession *)session downloadTask:( NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:( NSURL*)location

{

// location : 临时文件的路径(下载好的文件)

// NSString *caches = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) lastObject

// ];

NSString *url = [NSString stringWithFormat:@"/Users/0426ios/Desktop/test/aabbcc.mp4"];

// [filehandle writeData:downloadTask];

// response.suggestedFilename 建议使用的文件名,一般跟服务器端的文件名一致

NSString *file = [url stringByAppendingPathComponent :downloadTask. response . suggestedFilename

];

// 将临时文件剪切或者复制 Caches 文件夹

//判断下载的文件是否存在

NSFileManager *mgr = [ NSFileManager defaultManager];

if(![mgr fileExistsAtPath:url]){

//文件不存在

[mgr createFileAtPath:url contents:nil attributes:nil];

}else{

//文件存在

NSFileHandle *filehandle = [NSFileHandle fileHandleForUpdatingAtPath:url];

//在文件的末尾追加数据

[filehandle seekToEndOfFile];

[mgr moveItemAtPath :location. path toPath :file error : nil

];

[filehandle closeFile];

}

// AtPath : 剪切前的文件路径

// ToPath : 剪切后的文件路径

}

/**

恢复下载时调用

*/

- ( void )URLSession:( NSURLSession *)session downloadTask:( NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:( int64_t )fileOffset expectedTotalBytes:( int64_t)expectedTotalBytes

{

}

/**

正在下载

@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

{

double progress = ( double )totalBytesWritten / totalBytesExpectedToWrite;

NSLog ( @" 下载进度 ---%f", progress);

//判断文件是否存在

}

@end

优质内容筛选与推荐>>
1、百度网盘不限速下载网页版
2、手动跟踪函数的调用过程【转】
3、洛谷P1093 奖学金
4、Android高级工程师面试题整理
5、【JAVA】JAVAで各DBに接続する方法(JDBC)の纏め(未完結)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号