CXDownload

Swift断点续传下载,支持Objective-C。包含大文件下载,后台下载,杀死进程,重新启动时继续下载,设置下载并发数,监听网络改变等。

MIT License

Stars
10
Committers
5

中文版 | English Version

CXDownload

Realization of breakpoint transmission download with Swift, support Objective-C. Including large file download, background download, killing the process, continuing to download when restarting, setting the number of concurrent downloads, monitoring network changes and so on.

Preview

If you think it's okay, please give it a star

Explanation

  • CXDownloadManager.swift: The download task management
  • CXDownloadTaskProcessor.swift: The download task processor
  • CXDownloadModel.swift: The download model
  • CXDownloadDatabaseManager.swift: The download database management
  • FileUtils.swift: The file tool
  • Logger.swift: This outputs the log to the console
  • String+Cx.swift: This extends the cx_md5, cxd_sha2 properties for String
  • ...

Installation

CXDownload is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CXDownload'

Usage

Note: In order to better understand the usages, please check the project example.

Monitor download status and progress

func addNotification() {
    NotificationCenter.default.addObserver(self, selector: #selector(downloadStateChange(_:)), name: CXDownloadConfig.stateChangeNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressChange(_:)), name: CXDownloadConfig.progressNotification, object: nil)
}

@objc func downloadStateChange(_ noti: Notification) {
    guard let downloadModel = noti.object as? CXDownloadModel else {
        return
    }
    if downloadModel.state == .finish {
        CXDLogger.log(message: "filePath: \(downloadModel.localPath ?? "")", level: .info)
    } else if downloadModel.state == .error || downloadModel.state == .cancelled {
        if let stateInfo = downloadModel.stateInfo {
            CXDLogger.log(message: "error: \(stateInfo.code), message: \(stateInfo.message)", level: .info)
        }
    }
}

@objc func downloadProgressChange(_ noti: Notification) {
    guard let downloadModel = noti.object as? CXDownloadModel else {
        return
    }
    CXDLogger.log(message: "[\(downloadModel.url)] \(Int(downloadModel.progress * 100)) %", level: .info)
}

Download

  • Default download directory and file name.
CXDownloadManager.shared.download(url: urls[0])
downloadButton1.dl.download(url: urls[0])
  • Custom download directory and file name.
CXDownloadManager.shared.download(url: urls[1], toDirectory: "Softwares", fileName: "MacDict_v1.20.30.dmg")
downloadButton2.dl.download(url: urls[1], toDirectory: "Softwares", fileName: "MacDict_v1.20.30.dmg") 

Pause

CXDownloadManager.shared.pause(url: urls[0])
pauseButton1.dl.pauseTask(url: urls[0])

Cancel

CXDownloadManager.shared.cancel(url: urls[0])
cancelButton1.dl.cancelTask(url: urls[0])

Delete target file

CXDownloadManager.shared.deleteTaskAndCache(url: urls[0])
deleteButton1.dl.deleteTaskAndCache(url: urls[0])
CXDownloadManager.shared.deleteTaskAndCache(url: urls[1], atDirectory: "Softwares", fileName: "MacDict_v1.20.30.dmg")
deleteButton2.dl.deleteTaskAndCache(url: urls[1], atDirectory: "Softwares", fileName: "MacDict_v1.20.30.dmg")

Recommendation

  • FireKylin - FireKylin provides many utilities and rich extensions of Swift language.
  • MarsUIKit - MarsUIKit wraps some commonly used UI components.
  • RxListDataSource - RxListDataSource provides data sources for UITableView or UICollectionView.
  • CXNetwork-Moya - CXNetwork-Moya encapsulates a network request library with Moya and ObjectMapper.

Example

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

License

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

Feedback is welcome

If you notice any issue to create an issue. I will be happy to help you.

Package Rankings
Top 21.51% on Cocoapods.org
Badges
Extracted from project README
Version License Platform
Related Projects