Digger is a lightweight download framework that requires only one line of code to complete the file download task
Digger是一个轻量的下载框架,一行代码即可实现文件下载任务.
基于URLSession,纯Swift语言实现,支持链式语法调用,实时下载进度,实时下载速度,断点下载.
用户强制关闭app,例如上滑关闭app. Digger依然可以恢复下载任务.
Add to your Podfile:
use_frameworks!
pod 'Digger'
Add to your Cartfile:
github "cornerAnt/Digger"
###基本用法:
在沙盒的Caches下的Digger目录,下载一个文件
let url = "http://example.com/digger.mp4"
Digger.download(url)
###链式语法调用:
可以根据自己的需求,选择下载进度,下载速度,下载结果回调
Digger.download(url)
.progress(nil)
.speed(nil)
.completion(nil)
let url = "http://example.com/digger.mp4"
Digger.download(url)
.progress({ (progresss) in
print(progresss.fractionCompleted)
})
.speed({ (speed) in
print(speed)
})
.completion { (result) in
switch result {
case .success(let url):
print(url)
case .failure(let error):
print(error)
}
}
}
自定义配置:
/// 是否立刻开始任务,默认为true
DiggerManager.shared.startDownloadImmediately = false
/// 设置并发数,默认为3
DiggerManager.shared.maxConcurrentTasksCount = 4
/// 设置请求超时,默认为150毫秒
DiggerManager.shared.timeout = 150
/// 设置是否可用蜂窝数据下载,默认为true
DiggerManager.shared.allowsCellularAccess = false
/// 设置日志级别,默认为high,格式如下,设置为none则关闭
DiggerManager.shared.logLevel = .none
/*
***************DiggerLog****************
file : ExampleController.swift
method : viewDidLoad()
line : [31]:
info : digger log
*/
// MARK:- DiggerCache
/// 在沙盒cactes目录,自定义缓存目录
DiggerCache.cachesDirectory = "Directory"
/// 删除所有下载的文件
DiggerCache.cleanDownloadFiles()
/// 删除所有临时下载文件
DiggerCache.cleanDownloadTempFiles()
/// 获取系统可用内存
_ = DiggerCache.systemFreeSize()
/// 获取已下载文件大小
_ = DiggerCache.downloadedFilesSize()
/// 获取所有下载完成的文件的路径
_ = DiggerCache.pathsOfDownloadedfiles
}
欢迎 Fork , Star , Issues
cornerAnt
Digger is Copyright © 2017 cornerAnt and released as open source under the attached MITLICENSE.