TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jul 2016 |
SPMSupports SPM | ✗ |
Maintained by James Huynh.
Depends on: | |
FileMD5Hash | ~> 2.0.0 |
ReachabilitySwift | ~> 2.3.3 |
JHDownloadManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "JHDownloadManager", "~> 1.0.2"
JHDownloadManager
can perform download on a batch of URL strings or NSURL
objects.
url
can be string or NSURL
objectfileSize
is not supplied, the download manager will trigger a HEAD
request to query for content length to fill in fileSize
.fileSize
. Default file hashing algorithm is SHA1. You can change by usinglet downloadManager = JHDownloadManager.sharedInstance
downloadManager.fileHashAlgorithm = FileHashAlgorithm.MD5
import JHDownloadManager
let downloadManager = JHDownloadManager.sharedInstance
downloadManager.downloadBatch([
[
"url": "http://87.76.16.10/test10.zip",
"destination": "test/test10.zip",
"fileSize": 11536384,
"checksum": "5e8bbbb38d137432ce0c8029da83e52e635c7a4f",
"identifier": "Content-1001"
],
[
"url": "http://speedtest.dal01.softlayer.com/downloads/test100.zip",
"destination": "test/test100.zip",
"fileSize": 104874307,
"checksum": "592b849861f8d5d9d75bda5d739421d88e264900",
"identifier": "Content-1002"
]
])
downloadManager
instance first and then call startDownloadingCurrentBatch
laterdownloadManager.addBatch([
...
])
...
downloadManager.startDownloadingCurrentBatch()
let downloadManager = JHDownloadManager.sharedInstance
downloadManager.setInitialDownloadedBytes(1024)
let downloadManager = JHDownloadManager.sharedInstance
downloadManager.setTotalBytes(1048576)
let downloadManager = JHDownloadManager.sharedInstance
downloadManager = FileHashAlgorithm.MD5;
// downloadManager.fileHashAlgorithm = FileHashAlgorithm.SHA512;
// downloadManager.fileHashAlgorithm = FileHashAlgorithm.SHA1; // default
JHDownloadManagerUIDelegate
can be used to update progress of the batch download and update finish status of the whole batch
// let downloadManager = JHDownloadManager.sharedInstance
// downloadManager.uiDelegate = self;
// ...
func didReachProgress(progress:Float) {
// this method runs on main thread
// ... update progress bar or progress text here
}
func didFinishAll() {
// this method runs on main thread
// ... update completed status of the whole batch
}
func didFinishOnDownloadTaskUI(task:JHDownloadTask) {
// this method runs on main thread
// ... update completed status of a download task
}
func didReachIndividualProgress(progress:Float, task:JHDownloadTask) {
// this method runs on main thread
// ... update progress of a task
}
JHDownloadManagerDataDelegate
can be used to process file after finish downloading
// let downloadManager = JHDownloadManager.sharedInstance
// downloadManager.dataDelegate = self
// ...
func didFinishDownloadObject(task: JHDownloadTask) {
// this method runs on background thread
}
func didFinishAllForDataDelegate() {
// this method is run on background thread
// do whatever needs to be done after a batch has been downloaded successfully
}
In didFinishDownloadObject
you will receive an JHDownloadTask
instance. Inside this instance, you will be able to retrieve the following attributes
let url:NSURL = task.url
let urlString:String = task.urlString
let destination:String = task.destination // destination is the full path to the downloaded file
let fileName:String = task.fileName
let checksum:String = task.checksum
let identifier:String = task.identifier
let downloadManager = JHDownloadManager.sharedInstance
downloadManager.addDownloadTask(["url": "http://download.thinkbroadband.com/5MB.zip", "destination": "test/5MB.zip"])
let downloadManager = JHDownloadManager.sharedInstance
let currentDownloadTask = downloadManager.downloadingTasks()
let downloadRateAndRemaining = JHDownloadManager.sharedInstance.downloadRateAndRemainingTime()
let downloadRate:String = downloadRateAndRemaining[0];
let remainingTime:String = downloadRateAndRemaining[1];
let isDownloading = JHDownloadManager.sharedInstance.isDownloading()
git clone [email protected]:jameshuynh/JHDownloadManager.git
JHDownloadManager/Example/JHDownloadManager.xcworkspace
Cmd + R
to run the example project :-)Contribution, Suggestion and Issues are very much appreciated :). Please also fork and send your pull request!
James Huynh, [email protected]
JHDownloadManager is available under the MIT license. See the LICENSE file for more info.