• Stars
    star
    412
  • Rank 105,024 (Top 3 %)
  • Language
    Objective-C
  • Created over 8 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Download file manager wrapped NSURLSessionDataTask and NSURLSessionDownloadTask,provide progress update and status change.

TYDownloadManager

Download file manager wrapped NSURLSessionDataTask and NSURLSessionDownloadTask,provide progress update and status change.
下载管理类(TYDownLoadDataManager和TYDownloadSessionManager) 支持多文件断点下载和后台下载 封装了NSURLSessionDataTask和NSURLSessionDownloadTask,提供进度更新和状态改变block 和 delegate。


视频播放请看TYVideoPlayer

Requirements

  • Xcode 6 or higher
  • iOS 7.0 or higher
  • ARC

Features

  • TYDownLoadDataManager封装了NSURLSessionDataTask
  • TYDownloadSessionManager封装了NSURLSessionDownloadTask
  • 支持进度更新和状态改变block和delegate
  • 支持多文件下载和断点续传下载,TYDownloadSessionManager支持后台下载
  • 支持设置最大同时下载数maxDownloadCount和批量下载isBatchDownload
  • 支持等待下载队列 先进先出或先进后出 resumeDownloadFIFO

ScreenShot

image
image

Usage

API Quickstart

// 下载代理
@property (nonatomic,weak) id<TYDownloadDelegate> delegate;

// 下载中的模型 只读
@property (nonatomic, strong,readonly) NSMutableArray *waitingDownloadModels;

// 等待中的模型 只读
@property (nonatomic, strong,readonly) NSMutableArray *downloadingModels;

// 最大下载数
@property (nonatomic, assign) NSInteger maxDownloadCount;

// 等待下载队列 先进先出 默认YES, 当NO时,先进后出
@property (nonatomic, assign) BOOL resumeDownloadFIFO;

// 全部并发 默认NO, 当YES时,忽略maxDownloadCount
@property (nonatomic, assign) BOOL isBatchDownload;

// 单例
+ (TYDownLoadDataManager *)manager;

// 单例
+ (TYDownloadSessionManager *)manager;

// 开始下载
- (TYDownloadModel *)startDownloadURLString:(NSString *)URLString toDestinationPath:(NSString *)destinationPath progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state;

// 开始下载
- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel progress:(TYDownloadProgressBlock)progress state:(TYDownloadStateBlock)state;

// 开始下载
- (void)startWithDownloadModel:(TYDownloadModel *)downloadModel;

// 恢复下载(除非确定对这个model进行了suspend,否则使用start)
- (void)resumeWithDownloadModel:(TYDownloadModel *)downloadModel;

// 暂停下载
- (void)suspendWithDownloadModel:(TYDownloadModel *)downloadModel;

// 取消下载
- (void)cancleWithDownloadModel:(TYDownloadModel *)downloadModel;

// 删除下载
- (void)deleteFileWithDownloadModel:(TYDownloadModel *)downloadModel;

// 删除下载
- (void)deleteAllFileWithDownloadDirectory:(NSString *)downloadDirectory;

// 获取正在下载模型
- (TYDownloadModel *)downLoadingModelForURLString:(NSString *)URLString;
/**
 *  下载进度
 */
@interface TYDownloadProgress : NSObject

// 续传大小
@property (nonatomic, assign, readonly) int64_t resumeBytesWritten;
// 这次写入的数量
@property (nonatomic, assign, readonly) int64_t bytesWritten;
// 已下载的数量
@property (nonatomic, assign, readonly) int64_t totalBytesWritten;
// 文件的总大小
@property (nonatomic, assign, readonly) int64_t totalBytesExpectedToWrite;
// 下载进度
@property (nonatomic, assign, readonly) float progress;
// 下载速度
@property (nonatomic, assign, readonly) float speed;
// 下载剩余时间
@property (nonatomic, assign, readonly) int remainingTime;

@end

Block

// TYDownloadModel block
// 进度更新block
typedef void (^TYDownloadProgressBlock)(TYDownloadProgress *progress);
// 状态更新block
typedef void (^TYDownloadStateBlock)(TYDownloadState state,NSString *filePath, NSError *error);

Delegate

// TYDownLoadDataManager 和 TYDownloadSessionManager
// TYDownLoadManager下载代理
@protocol TYDownloadDelegate <NSObject>

// 更新下载进度
- (void)downloadModel:(TYDownloadModel *)downloadModel didUpdateProgress:(TYDownloadProgress *)progress;
// 更新下载状态
- (void)downloadModel:(TYDownloadModel *)downloadModel didChangeState:(TYDownloadState)state filePath:(NSString *)filePath error:(NSError *)error;

@end

Demo

// 下载可以使用 manager的代理 或者 downloadModel的block

- (IBAction)download:(id)sender {
    TYDownloadSessionManager *manager = [TYDownloadSessionManager manager];
    
    if (_downloadModel.state == TYDownloadStateReadying) {
        [manager cancleWithDownloadModel:_downloadModel];
        return;
    }
    
    if ([manager isDownloadCompletedWithDownloadModel:_downloadModel]) {
        [manager deleteFileWithDownloadModel:_downloadModel];
    }
    
    if (_downloadModel.state == TYDownloadStateRunning) {
        [manager suspendWithDownloadModel:_downloadModel];
        return;
    }
    [self startDownlaod];
}

- (void)startDownlaod
{
    TYDownloadSessionManager *manager = [TYDownloadSessionManager manager];
    __weak typeof(self) weakSelf = self;
    [manager startWithDownloadModel:_downloadModel progress:^(TYDownloadProgress *progress) {
        weakSelf.progressView.progress = progress.progress;
        weakSelf.progressLabel.text = [weakSelf detailTextForDownloadProgress:progress];
        
    } state:^(TYDownloadState state, NSString *filePath, NSError *error) {
        if (state == TYDownloadStateCompleted) {
            weakSelf.progressView.progress = 1.0;
            weakSelf.progressLabel.text = [NSString stringWithFormat:@"progress %.2f",weakSelf.progressView.progress];
        }
        
        [weakSelf.downloadBtn setTitle:[weakSelf stateTitleWithState:state] forState:UIControlStateNormal];
        
        //NSLog(@"state %ld error%@ filePath%@",state,error,filePath);
    }];
}

// [TYDownloadSessionManager manager].delegate = self;
// [TYDownLoadDataManager manager].delegate = self;

#pragma mark - TYDownloadDelegate

- (void)downloadModel:(TYDownloadModel *)downloadModel didUpdateProgress:(TYDownloadProgress *)progress
{
     NSLog(@"delegate progress %.3f",progress.progress);
}

- (void)downloadModel:(TYDownloadModel *)downloadModel didChangeState:(TYDownloadState)state filePath:(NSString *)filePath error:(NSError *)error
{
    NSLog(@"delegate state %ld error%@ filePath%@",state,error,filePath);
}

Contact

如果你发现bug,please pull reqeust me
如果你有更好的改进,please pull reqeust me

More Repositories

1

TYAttributedLabel

TYAttributedLabel 简单,强大的属性文本控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示
Objective-C
2,890
star
2

TYCyclePagerView

a simple and usefull cycle pager view ,and auto scroll banner view(轮播图) ,include pageControl for iOS,support Objective-C and swift
Objective-C
1,755
star
3

DOPDropDownMenu-Enhanced

DOPDropDownMenu 添加双列表 优化版 新增图片支持(double tableView, The optimization version ,new add image,detailText)
Objective-C
1,748
star
4

TYPagerController

page scroll view and controller,simple,high custom,and have many tabBar styles,,support Objective-C and swift
Objective-C
1,375
star
5

TYAlertController

Powerful, Easy to use alert view or popup view on controller and window, support blur effects,custom view and animation,for objective-c,support iphone, ipad
Objective-C
1,312
star
6

LovePlayNews

LovePlayNews精仿爱玩iOS app,使用AsyncDisplayKit提高UI流畅性,项目结构及代码清晰明了
Objective-C
668
star
7

TYWaterWaveView

水波浪圆形进度控件,采用 CAShapeLayer,CADisplayLink 波浪动画,简单,流畅
Objective-C
398
star
8

TReaderBook

图文混排阅读器
Objective-C
276
star
9

TYLaunchAnimation

launching image or view Animation,UIView Category,easy to use.(启动图动画,带广告, 直接使用,支持自定义view,自定义动画)
Objective-C
271
star
10

KnowingLife

基于天气,查询,团购,新闻类查询应用,自己自学ios,写的第一个ios项目,也是面试作品,好久没用了,放着可惜,拿来给大家分享,希望给想写个项目,又无处着手的新人帮助,新手作品
Objective-C
140
star
11

TYText

text asynchronous rendering by TextKit for iOS
Objective-C
135
star
12

Prism

Application Performance Management & Monitoring for iOS (APM)
Objective-C
71
star
13

TYSwizzleDemo

method swizzling easy to use ,C Function and NSObject Categary
Objective-C
13
star
14

TYHttpManager

网络请求管理库,基于对AFNetwork的封装
Objective-C
13
star
15

TYDecorationSectionLayout

custom collectionView sections background view (decoration view)
Objective-C
11
star
16

WeiboDemo

Weibo demo using asynchronous rendering by TYText
Objective-C
10
star
17

TYJSONModel

A fast conversion between JSON and Model
Objective-C
7
star
18

BulletAnalyzer

斗鱼弹幕分析工具, 语义近似度合并, CG/CA图表数据绘制, Socket链接
Objective-C
5
star
19

TYPopupController

TYPopupController is presenting view controller or view as popups of other view controller
Objective-C
5
star
20

TYRefresh

a simple way to use pull-to-refresh (上下拉刷新),easy way to customize loading animation(自定义加载动画).
Objective-C
3
star
21

TYBarrageManager

barrage manager view (弹幕)
Objective-C
1
star
22

12207480.github.io

HTML
1
star
23

iOS-Txt-Core

A simple demo for UIPageViewController.
Objective-C
1
star
24

xinWeibo-demo

sina weibo demo 新浪微博 demo
Objective-C
1
star