美文网首页
使用URLSession实现断点续传

使用URLSession实现断点续传

作者: alitan2015 | 来源:发表于2018-09-29 17:08 被阅读0次

一图简单了解URLSession,所有的网络请求任务都是由URLSession创建并挂起

思路图晚点补上,先上代码

下载管理器代码

class DownLoadManager:NSObject{

    static let instance: DownLoadManager = DownLoadManager()

    /// 单例

    ///

    /// -Returns: 返回对象本身

    class func sharedManager() ->DownLoadManager{

        return instance

    }

    /// 存储下载任务

    lazy var downLoadCache:Dictionary = {

        let dic =Dictionary()

        return dic

    }()

   /// 根据URL下载任务

   ///

   /// - Parameters:

   ///  - url: 链接

   ///  - progress: 进度

   ///  - compelete: 完成回调

   ///  - faile: 失败回调

   func downLoadFile(withUrl url:String,progress:@escaping(_progress:Float)->Void,compelete:@escaping(_filePath:String)->Void,faile:@escaping(_error:String)->Void) ->Void{

    let downLoadHepler =downLoadCache[url]

    if downLoadHepler !=nil {

        debugPrint("此任务下载中。。。")

    }else{

        let downLoadHepler =DownLoaderHepler()

        downLoadCache[url] = downLoadHepler

        downLoadHepler.downLoadFile(withUrl: url, progress: progress, compelete: { (filePath) in

            self.downLoadCache.removeValue(forKey: url)

            compelete(filePath)

        }, faile: faile)

    }

    }

    /// 暂停

    ///

    /// -Parameterurl: 链接

    func pasume(withUrl url:String) ->Void{

        let downLoadHepler =downLoadCache[url]

        if downLoadHepler !=nil {

            downLoadHepler?.pasume()

        }else{

            debugPrint("此任务不存在\(url)")

        }

    }

    /// 继续

    ///

    /// -Parameterurl: 继续链接

    func resume(withUrl url:String) ->Void{

        let downLoadHepler =downLoadCache[url]

        if downLoadHepler !=nil{

            downLoadHepler?.resume()

        }else{

            debugPrint("此任务不存在\(url)")

        }

    }

}

DownLoadManager下载代码

class DownLoadManager:NSObject{

    static let instance: DownLoadManager = DownLoadManager()

    /// 单例

    ///

    /// -Returns: 返回对象本身

    class funcsharedManager() ->DownLoadManager{

        return instance

    }

    /// 存储下载任务

    lazy var downLoadCache:Dictionary = {

        let dic =Dictionary()

        return dic

    }()

   /// 根据URL下载任务

   ///

   /// - Parameters:

   ///  - url: 链接

   ///  - progress: 进度

   ///  - compelete: 完成回调

   ///  - faile: 失败回调

   func downLoadFile(withUrl url:String,progress:@escaping(_progress:Float)->Void,compelete:@escaping(_filePath:String)->Void,faile:@escaping(_error:String)->Void) ->Void{

    let downLoadHepler =downLoadCache[url]

    if downLoadHepler !=nil{

        debugPrint("此任务下载中。。。")

    }else{

        let downLoadHepler =DownLoaderHepler()

        downLoadCache[url] = downLoadHepler

        downLoadHepler.downLoadFile(withUrl: url, progress: progress, compelete: { (filePath)in

            self.downLoadCache.removeValue(forKey: url)

            compelete(filePath)

        }, faile: faile)

    }

    }

    /// 暂停

    ///

    /// -Parameterurl: 链接

    func pasume(withUrl url:String) ->Void{

        let downLoadHepler =downLoadCache[url]

        if downLoadHepler !=nil{

            downLoadHepler?.pasume()

        }else{

            debugPrint("此任务不存在\(url)")

        }

    }

    /// 继续

    ///

    /// -Parameterurl: 继续链接

    func resume(withUrl url:String) ->Void{

        let downLoadHepler =downLoadCache[url]

        if downLoadHepler !=nil{

            downLoadHepler?.resume()

        }else{

            debugPrint("此任务不存在\(url)")

        }

    }

}

相关文章

网友评论

      本文标题:使用URLSession实现断点续传

      本文链接:https://www.haomeiwen.com/subject/zjdboftx.html