zoukankan      html  css  js  c++  java
  • supersr--NSURLSessionConfiguration-下载进度

    //
    //  ViewController.m
    //  下载进度
    //
    //  Created by Super on 14/7/4.
    //  Copyright (c) 2014年 iOS. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController () <NSURLSessionDownloadDelegate>
    @property (nonatomic, strong) NSURLSession *session;

    @property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;

    @property (nonatomic, strong) NSData *data;
    @property (weak, nonatomic) IBOutlet UIProgressView *processView;
    @end
    @implementation ViewController

    - (NSURLSession *)session{
        if (_session == nil) {
            //代理方法运行的队列
            NSOperationQueue *queue =  [NSOperationQueue mainQueue];
    //        //最大的并发线程数  ???????
            //在NSURLSession中使用NSOperationQueue设置最大并发数,无效
    //        queue.maxConcurrentOperationCount = 1;
           
           
            NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
            //创建自定义session  设置代理
            //session  会话
            _session = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:queue];
        }
        return _session;
    }

    //开始下载
    - (IBAction)start:(id)sender {
        [self download];
    }
    //暂停pause 下载
    - (IBAction)pause:(id)sender {
       
    //    [self.downloadTask cancel];
       
        NSLog(@"zan ting");
        //取消下载,并且存储当前下载的数据
        [self.downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
            self.data = resumeData;
        }];
        //如果点击多次暂停 会出问题  无法继续(因为 点击多次暂停的话 resumeData 为空)
        self.downloadTask = nil;
      
    }
    //继续下载
    - (IBAction)resume:(id)sender {
        if (self.data == nil) {
            NSLog(@"没有要继续的数据");
            return;
        }
       
        [[self.session downloadTaskWithResumeData:self.data] resume];
       
        self.data = nil;
    }
    - (void)download{
    //    NSString *strUrl = @"http://127.0.0.1/A02-head.mp4";
        NSString *strUrl = @"http://dlsw.baidu.com/sw-search-sp/soft/9d/25765/sogou_mac_31.1421982306.dmg";
        strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:strUrl];
        //使用代理的话,不能用此方法 
    //    [[self.session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    //        NSLog(@"---%@",[NSThread currentThread]);
    //    }] resume];
       
        //执行代理方法
        self.downloadTask = [self.session downloadTaskWithURL:url];
        [self.downloadTask resume];
    //     [[self.session downloadTaskWithURL:url] resume];
    }

    //代理的方法
     
    //下载完成  —————>>>  didFinishDownloadingToURL
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
        NSLog(@"over");
    }
    //断点续传    ————>>>  didResumeAtOffset
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{
        NSLog(@"==");
    }
    //进度
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
        NSLog(@"---%@",[NSThread currentThread]);

    //    [NSThread sleepForTimeInterval:0.1];
        //bytesWritten 本次下载了多少
        //totalBytesWritten  总共下载了多少
        //totalBytesExpectedToWrite  文件的大小
        float process = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
        NSLog(@"%f", process);
        self.processView.progress = process;
       
    }
     
    @end
  • 相关阅读:
    【JVM】程序计数器(四)
    【JVM】运行时数据区概述及线程(三)
    【JVM】类加载子系统(二)
    MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###
    计算机常用指令大全
    HTML的设计与应用
    想得太多,又做的太少
    互联网数据库分库分表现状及一点思考
    python中的函数与文件操作
    python中的流程控制语句
  • 原文地址:https://www.cnblogs.com/supersr/p/4854337.html
Copyright © 2011-2022 走看看