Skip to content

HolyBuz/FileDownloader

Repository files navigation

FileDownloader

A simple and powerful framework for downloading files in Swift.

Features

  • Closure-based and Combine-based APIs for flexibility.
  • Pause, resume, and cancel downloads.
  • Progress tracking
  • Caching for performance
  • Error handling
  • Easy file retrieval

🔨 Technologies: Swift, Combine, No Third Party libraries.

🚀Platform: 📱iOS & 🖥️MacOS

Requirements

  • iOS 13.0+
  • Xcode 11.0+

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Getting Started

Using CocoaPods

To install it, simply add the following line to your Podfile:

pod 'FileDownloader'

Using Swift Package Manager:

  1. Add the following to your Package.swift file:
dependencies: [
    .package(url: "https://github.com/HolyBuz/FileDownloader")
]
  1. Run swift package update to fetch the package.

Usage

  1. Import the framework:
import FileDownloader
  1. Create a File object:
let file = File(url: URL(string: "https://example.com/file.zip")!)
  1. 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)
    }
}
  1. Pause, resume, or cancel downloads:
FileHandler.pause(file: file)
FileHandler.resume(file: file)
FileHandler.cancel(file: file)
  1. Retrieve a downloaded file:
let localFileURL = FileHandler.get(file: file)

Additional Information

  • Caching: Files are cached by default using the downloadConfiguration parameter.
  • Error handling: Handle errors appropriately using the completion handlers or Combine's sink operator.

Author

HolyBuz, holybuz@gmail.com

License

FileDownloader is available under the MIT license. See the LICENSE file for more info.

Thanks for stopping by!