Code Monkey home page Code Monkey logo

fkdownloader's Introduction

FKDonwloader

Support Language Carthage Compatible CocoaPods Compatible

👍🏻📥也许是最好的文件下载器.

Features

  • 后台下载
  • 恢复所有后台任务和进度
  • 自管理任务持久化
  • 使用配置实例统一配置
    • 可配置是否为后台下载
    • 可配置是否允许蜂窝网络下载
    • 可配置自动开始/自动清理
    • 可配置并行任务数量
    • 可配置自定义保存/缓存/恢复目录
    • 可配置超时时间
    • 可配置是否进行文件校验
  • 使用 NSProgress 管理任务进度
  • 所有任务总进度
  • 任务实时速度和预计剩余时间
  • 文件校验, 支持 MD5/SHA1/SHA256/SHA512, 并对特大文件校验进行了内存优化
  • 自定义文件名
  • 任务可添加多个 Tag, 并通过 Tag 进行分组
  • 通过 Tag 获取组任务进度
  • 状态与进度数据可通过代理/通知/Block任意获取
  • 网络状态检测, 恢复网络时自动开始被中断的任务
  • 没有使用任何第三方
  • 自启动, 无需显式调用
  • 兼容 Swift
  • 更简单的调用
  • 更详细的任务状态: 无/预处理/等待/进行中/完成/取消/暂停/恢复/校验/错误

初衷与动机

一个系统BUG引发的血案

简单使用 -- ObjC

  • 任务管理
// 批量添加任务, 数组元素支持 NSString, NSURL, NSDictionary, NSMutableDictionary
[[FKDownloadManager manager] addTaskWithArray:urls];

// 批量添加任务并分配 tag
[[FKDownloadManager manager] addTaskWithArray:urls tag:@"group_task_01"];

// 开始执行任务
[[FKDownloadManager manager] start:@"URL"];

// 根据 URL 获取任务
[[FKDownloadManager manager] acquire:@"URL"];

// 暂停任务
[[FKDownloadManager manager] suspend:@"URL"];

// 恢复任务
[[FKDownloadManager manager] resume:@"URL"];

// 取消任务
[[FKDownloadManager manager] cancel:@"URL"];

// 移除任务
[[FKDownloadManager manager] remove:@"URL"];

// 设置任务代理
[[FKDownloadManager manager] acquire:@"URL"].delegate = self;

// 设置任务 Block
[[FKDownloadManager manager] acquire:@"URL"].statusBlock = ^(FKTask *task) {
    // 状态改变时被调用
};
[[FKDownloadManager manager] acquire:@"URL"].speedBlock = ^(FKTask *task) {
    // 下载速度, 默认 1s 调用一次
};
[[FKDownloadManager manager] acquire:@"URL"].progressBlock = ^(FKTask *task) {
    // 进度改变时被调用
};
  • 支持的任务通知
// 与代理同价, 可按照代理的使用方式使用通知.
extern FKNotificationName const FKTaskPrepareNotification;
extern FKNotificationName const FKTaskDidIdleNotification;
extern FKNotificationName const FKTaskWillExecuteNotification;
extern FKNotificationName const FKTaskDidExecuteNotication;
extern FKNotificationName const FKTaskProgressNotication;
extern FKNotificationName const FKTaskDidResumingNotification;
extern FKNotificationName const FKTaskWillChecksumNotification;
extern FKNotificationName const FKTaskDidChecksumNotification;
extern FKNotificationName const FKTaskDidFinishNotication;
extern FKNotificationName const FKTaskErrorNotication;
extern FKNotificationName const FKTaskWillSuspendNotication;
extern FKNotificationName const FKTaskDidSuspendNotication;
extern FKNotificationName const FKTaskWillCancelldNotication;
extern FKNotificationName const FKTaskDidCancelldNotication;
extern FKNotificationName const FKTaskSpeedInfoNotication;
extern FKNotificationName const FKTaskWillRemoveNotification;
extern FKNotificationName const FKTaskDidRemoveNotification;
  • 需要在 AppDelegate 中调用的
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler {
    
    // 保存后台下载所需的系统 Block, 区别 identifier 以防止与其他第三方冲突
    if ([identifier isEqualToString:[FKDownloadManager manager].configure.sessionIdentifier]) {
        [FKDownloadManager manager].configure.backgroundHandler = completionHandler;
    }
}

简单使用 -- Swift

  • 任务管理
// 批量添加任务, 数组元素支持 NSString, NSURL, NSDictionary, NSMutableDictionary
Downloader.shared().addTasks(with: urls)

// 批量添加任务并分配 tag
Downloader.shared().addTasks(with: urls, tag: "group_task_01")

// 开始执行任务
Downloader.shared().start("URL")

// 根据 URL 获取任务
Downloader.shared().acquire("URL")

// 暂停任务
Downloader.shared().suspend("URL")

// 恢复任务
Downloader.shared().resume("URL")

// 取消任务
Downloader.shared().cancel("URL")

// 移除任务
Downloader.shared().remove("URL")

// 设置任务代理
Downloader.shared().acquire("URL")?.delegate = self

// 设置任务 Block
Downloader.shared().acquire("URL")?.statusBlock = { (task) in
    // 状态改变时被调用
}
Downloader.shared().acquire("URL")?.progressBlock = { (task) in
    // 下载速度, 默认 1s 调用一次
}
Downloader.shared().acquire("URL")?.speedBlock = { (task) in
    // 进度改变时被调用
}
  • 支持的任务通知
// 与代理同价, 可按照代理的使用方式使用通知.
extension NSNotification.Name {

    public static let FKTaskPrepare: NSNotification.Name

    public static let FKTaskDidIdle: NSNotification.Name

    public static let FKTaskWillExecute: NSNotification.Name

    public static let FKTaskDidExecute: NSNotification.Name

    public static let FKTaskProgress: NSNotification.Name

    public static let FKTaskDidResuming: NSNotification.Name

    public static let FKTaskWillChecksum: NSNotification.Name

    public static let FKTaskDidChecksum: NSNotification.Name

    public static let FKTaskDidFinish: NSNotification.Name

    public static let FKTaskError: NSNotification.Name

    public static let FKTaskWillSuspend: NSNotification.Name

    public static let FKTaskDidSuspend: NSNotification.Name

    public static let FKTaskWillCancelld: NSNotification.Name

    public static let FKTaskDidCancelld: NSNotification.Name

    public static let FKTaskSpeedInfo: NSNotification.Name
    
    public static let FKTaskWillRemove: NSNotification.Name

    public static let FKTaskDidRemove: NSNotification.Name
}
  • 需要在 AppDelegate 中调用的
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        
    if identifier == Downloader.shared().configure.sessionIdentifier {
        Downloader.shared().configure.backgroundHandler = completionHandler
    }
}

示例/最佳实践

请直接运行 Demo.   

安装

  • CocoaPods
      pod 'FKDownloader'
  • Carthage
      github 'SYFH/FKDownloader'
  • Manual
      将FKDownloader 文件夹复制到项目中, #import "FKDownloader.h" 即可开始

关于

如果觉得好用, 可以 Star 哟~
如果觉得功能不如人意, 请尽情的 Fork!
如果使用中出现了问题, 请直接提交 issues!
  

MIT License

Copyright (c) 2018 Norld

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fkdownloader's People

Contributors

syfh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.