zoukankan      html  css  js  c++  java
  • 网络编程(三) 下载任务,支持断点续传

    图:

    gitHub地址:https://github.com/wl356485255/DownloadTaskDemo.git

    注意点:1.下载的文件会保存在沙盒目录下的tmp文件夹内,这个文件夹专门存储一些临时文件,我们需要在下载完成后把文件移动到自己需要的文件夹内,并修改文件的扩展名

    2.有部分链接使用下载时resume data = nil;这样断点续传功能就没有用了;

    在storyboard里面添加button,progressView和显示进度的label,并进行连线

    ViewController.m代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    #import "ViewController.h"
     
    @interface ViewController ()<
    NSURLSessionDownloadDelegate
    >
    {
        NSURLSessionDownloadTask *downTask;
        NSData *myResumeData;
        NSURLSession *session;
        NSString *fielName;
         
    }
     
    @property (weak, nonatomicIBOutlet UIProgressView *progressView;
    @property (weak, nonatomicIBOutlet UILabel *progressLabel;
     
    @end
     
    @implementation ViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
     
     
    //创建下载任务
    - (IBAction)downloadAction:(id)sender
    {
        if (downTask)
        {
            return;
        }
        //构造需要下载的url链接
         
        //设置保存文件名为url的最后部分
        fielName = [url lastPathComponent];
        //设置session工作类型为默认
        NSURLSessionConfiguration *sessionCf = [NSURLSessionConfiguration defaultSessionConfiguration];
        //用sessionConfig配置session
        session = [NSURLSession sessionWithConfiguration:sessionCf delegate:self delegateQueue:[NSOperationQueue mainQueue]];
        //设置任务类型
        downTask = [session downloadTaskWithURL:url];
     
        //发送任务请求
        [downTask resume];
         
    }
     
     
     
    //暂停下载任务
    - (IBAction)pauseAction:(UIButton *)sender
    {
        if (downTask.state == NSURLSessionTaskStateRunning)
        {
            [downTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
                //If resume data cannot be created, the completion handler will be called with nil resumeData
                myResumeData = resumeData;
                downTask = nil;
            }];
        }
         
    }
    //恢复下载任务
    - (IBAction)resumeAction:(id)sender
    {
        if (myResumeData)
        {
            downTask = [session downloadTaskWithResumeData:myResumeData];
            [downTask resume];
             
            myResumeData = nil;
        }
    }
    //把下载来的文件移动到Documents文件夹下并修改扩展名
    - (void)moveFileToDocuments:(NSURL *)location
    {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *newPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fielName]];
        fielName = nil;
        NSLog(@"%@",newPath);
         
        [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:newPath] error:nil];
         
    }
     
    #pragma mark - NSURLSessionDownloadDelegate
     
    //在下载完成后调用
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
    {
        NSLog(@"%@",location);
        [self moveFileToDocuments:location];
    }
     
    //进度条相关设置
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
    {
        float progress = (float)totalBytesWritten / totalBytesExpectedToWrite;
        self.progressView.progress = progress;
        self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress* 100];
         
    }
  • 相关阅读:
    登录功能通用测试用例
    sql中的 where 、group by 和 having 用法解析(摘抄)
    PL/SQL链接数据库
    Ubuntu 14.04下搭建SVN服务器(SVN Server),摘抄过来,以防万一
    VMware® Workstation 12 Pro Linux Ubuntu 中subversion的服务器搭建
    到底EJB是什么
    xml操作-Nested exception: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. 异常处理
    Hibernate 延迟加载剖析与代理模式应用
    CommandBehavior.CloseConnection使用
    hibernate 级联删除报更新失败的问题(org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)
  • 原文地址:https://www.cnblogs.com/luoxiaofu/p/5220753.html
Copyright © 2011-2022 走看看