A simple and powerful framework for downloading files in Swift.
- Closure-based and Combine-based APIs for flexibility.
- Pause, resume, and cancel downloads.
- Progress tracking
- Caching for performance
- Error handling
- Easy file retrieval
- iOS 13.0+
- Xcode 11.0+
To run the example project, clone the repo, and run pod install
from the Example directory first.
To install it, simply add the following line to your Podfile:
pod 'FileDownloader'
- Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/HolyBuz/FileDownloader")
]
- Run swift package update to fetch the package.
- Import the framework:
import FileDownloader
- Create a File object:
let file = File(url: URL(string: "https://example.com/file.zip")!)
- Start the download:
Using closures:
FileHandler.download(file: file) { progress in
print("Progress:", progress)
} onCompletion: { result in
switch result {
case .success(let locationURL):
print("Download complete:", locationURL)
case .failure(let error):
print("Error:", error)
}
}
Using Combine:
let publisher = DownloadTaskPublisher(file: file)
publisher.sink { completion in
print("Completion:", completion)
} receiveValue: { event in
switch event {
case .progress(let percentage):
print("Progress:", percentage)
case .url(let locationURL):
print("Location URL:", locationURL)
}
}
- Pause, resume, or cancel downloads:
FileHandler.pause(file: file)
FileHandler.resume(file: file)
FileHandler.cancel(file: file)
- Retrieve a downloaded file:
let localFileURL = FileHandler.get(file: file)
- Caching: Files are cached by default using the downloadConfiguration parameter.
- Error handling: Handle errors appropriately using the completion handlers or Combine's sink operator.
HolyBuz, holybuz@gmail.com
FileDownloader is available under the MIT license. See the LICENSE file for more info.