FileDownloadingCenter 1.1.4

FileDownloadingCenter 1.1.4

Amr El-Sayed 维护。



  • Amr Elsayed

FileDownloadingCenter

Version License Platform

Screenshot Screenshot

File Downloading Center是用于文件下载的iOS库。

特性

根据对象下载文件。根据URL下载文件。检索当前的下载项。使用通知中心监控下载过程。使用代理监控下载过程。配置互联网源(3G/无线)。配置任何问题的通知消息。在应用程序中断时恢复和继续正在下载的项。

用法

以下是与该库协同工作所需的最少示例,但实际上您可以根据需要的详细程度直接处理DownloaderManager或DownloaderQueue。

1 - 遵循“DownloadableItem”协议。

class Track: DownloadableItem {

    var downloadUrl : URL
    var downloadableItemDescription : Description
    var downloadFileLocation : URL
    // MARK:- Optional Functions
    var downloadableItemIdentifier : String
    var itemGroupBy : String?
    func isItemDownloaded() -> Bool

}

2- 遵循“SessionConfigurationProtocol”协议。

class DownloadSessionConfiguration : SessionConfigurationProtocol {
    
        public static let instance = DownloadSessionConfiguration()
        
        var sessionBackgroundIdentifier: String = "Your.Identifier.background"
        var isDiscretionary: Bool = false
        var allowsCellularAccess: Bool = true
    
}

3- 遵循“DownloadableItemProvider”协议。

class MyDownloadableItemProvider : DownloadableItemProvider {
    
    public static let shared = MyDownloadableItemProvider()
    
    func downloadableItem(withIdentifier identifier: String) -> DownloadableItem? {
        
        // Return your item using the identifier (URL) 
        
    }
    
    func downloadableItem(withUrl url: URL) -> DownloadableItem? {
        
        // Return your item using the identifier (URL)
        
    }
    
}

4- 创建您的FileDownloadManager,继承“ModelDownloaderManager”类。

class MyDownloaderManager : ModelDownloaderManager {
    
    public static var shared = MyDownloaderManager()
    
    override var noInternetConnectionMessage: String {
        
        "No Internet Connection !".localized()
        
    }
    
    override var celluarNetworkInternetConnectionMessage: String {
        
        "Connection may be not allow to establish this downloading session, you may review settings page to enable downloading over cellular connection".localized()
        
    }
    
    override var sessionConfiguration: SessionConfigurationProtocol {
        
        return DownloadSessionConfiguration.instance
        
    }
   
    override var downloadableItemProvider: DownloadableItemProvider? {
    
        return MyDownloadableItemProvider.shared
        
    }
    
    override func allowCelluarNetworkDownload() -> Bool {
        
       return false
        
    }
    
    override func showErrorMessage(errorMessage: String) {
        
        // Show the error Message.
        
    }
    
    override func updateItemDownloadFlag(itemIndentifier: String, isDownloaded: Bool) {
        
        // item state is changed to isDownloaded, do your stuff.
        
    }
    
}

5- 在AppDelegate中调用此函数,func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

这是为了恢复中断的下载。

private func initDownloaderManager() {
       
    let _ = MyDownloaderManager.shared
    
    DownloaderManager.shared.restore(downloadableItemProvider: MyDownloadableItemProvider.shared, configuration: DownloadSessionConfiguration.instance, finished: { _ in
               
        // Do your stuff
                    
    })
           
}

6- 遵循“DownloaderListener”协议以监听和处理下载进度的回调

Class MyViewController : DownloaderListener {

    func addDownloadObserver() {
           
           MyDownloaderManager.shared.addListener(downloaderListener: self, forDownloadableItems: self.downloadableItems)
           
    }

    func removeObservers() {
         
         MyDownloaderManager.shared.removeListener(downloaderListener: self)
           
    }
       
       // MARK:- Delegates
       
       private func isCurrentDownloader(downloader: FileDownloader) -> Bool {
           
           return downloader.identifier == self.downloadableItem?.downloadableItemIdentifier ?? ""
       }
       
       func downloader(didChangeState fileDownloader: FileDownloader) {
           
           if self.isCurrentDownloader(downloader: fileDownloader) {
           
               if let state = fileDownloader.state
               {
                   switch state {
                       
                   case .error:
                   // Do Your stuff.
                       
                    case .completed:
                    // Do Your stuff.
                       
                   default:
                       break
                       
                   }
               }
            }
       }
       
       func downloader(didUpdateProgress fileDownloader: FileDownloader) {
           
           if self.isCurrentDownloader(downloader: fileDownloader) {
               
               let progress = fileDownloader.progress!
               // Do Your stuff.
               
           }
           
       }
       
       func queue(didUpdateProgress downloaderQueue: DownloaderQueue) {
           
       }
       
       func file(didDelete downloadableItem: DownloadableItem, withError hasError: Bool) {
         
       }
       
}

示例

要运行示例项目,请先复制repo,然后从Example目录运行pod install

需求

iOS 10.0+ Xcode 10.2+ Swift 5+

安装

CocoaPods

FileDownloadingCenter 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'FileDownloadingCenter', '~> 1.1'

Swift 包管理器

Swift 包管理器是一个用于自动化 Swift 代码分发的工具,它是集成到 swift 编译器中的。

dependencies: [
.package(url: "https://github.com/amr-abdelfattah/iOS-FileDownloadingCenter.git", from: "1.1.4")
]

许可

FileDownloadingCenter 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。