中文版 | English Version
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.
If you think it's okay, please give it a
star
- 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 forString
- ...
CXDownload
is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CXDownload'
Note: In order to better understand the usages, please check the project example.
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)
}
- 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")
CXDownloadManager.shared.pause(url: urls[0])
pauseButton1.dl.pauseTask(url: urls[0])
CXDownloadManager.shared.cancel(url: urls[0])
cancelButton1.dl.cancelTask(url: urls[0])
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")
- 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.
To run the example project, clone the repo, and run pod install
from the Example directory first.
CXDownload
is available under the MIT license. See the LICENSE file for more info.
If you notice any issue to create an issue. I will be happy to help you.