TYDownLoadModel.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // TYDownloadModel.h
  3. // TYDownloadManagerDemo
  4. //
  5. // Created by tany on 16/6/1.
  6. // Copyright © 2016年 tany. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. // 下载状态
  10. typedef NS_ENUM(NSUInteger, TYDownloadState) {
  11. TYDownloadStateNone, // 未下载 或 下载删除了
  12. TYDownloadStateReadying, // 等待下载
  13. TYDownloadStateRunning, // 正在下载
  14. TYDownloadStateSuspended, // 下载暂停
  15. TYDownloadStateCompleted, // 下载完成
  16. TYDownloadStateFailed // 下载失败
  17. };
  18. @class TYDownloadProgress;
  19. @class TYDownloadModel;
  20. // 进度更新block
  21. typedef void (^TYDownloadProgressBlock)(TYDownloadProgress *progress);
  22. // 状态更新block
  23. typedef void (^TYDownloadStateBlock)(TYDownloadState state,NSString *filePath, NSError *error);
  24. /**
  25. * 下载模型
  26. */
  27. @interface TYDownloadModel : NSObject
  28. // >>>>>>>>>>>>>>>>>>>>>>>>>> download info
  29. // 下载地址
  30. @property (nonatomic, strong, readonly) NSString *downloadURL;
  31. // 文件名 默认nil 则为下载URL中的文件名
  32. @property (nonatomic, strong, readonly) NSString *fileName;
  33. // 缓存文件目录 默认nil 则为manger缓存目录
  34. @property (nonatomic, strong, readonly) NSString *downloadDirectory;
  35. // >>>>>>>>>>>>>>>>>>>>>>>>>> task info
  36. // 下载状态
  37. @property (nonatomic, assign, readonly) TYDownloadState state;
  38. // 下载任务
  39. @property (nonatomic, strong, readonly) NSURLSessionTask *task;
  40. // 文件流
  41. @property (nonatomic, strong, readonly) NSOutputStream *stream;
  42. // 下载进度
  43. @property (nonatomic, strong ,readonly) TYDownloadProgress *progress;
  44. // 下载路径 如果设置了downloadDirectory,文件下载完成后会移动到这个目录,否则,在manager默认cache目录里
  45. @property (nonatomic, strong, readonly) NSString *filePath;
  46. // >>>>>>>>>>>>>>>>>>>>>>>>>> download block
  47. // 下载进度更新block
  48. @property (nonatomic, copy) TYDownloadProgressBlock progressBlock;
  49. // 下载状态更新block
  50. @property (nonatomic, copy) TYDownloadStateBlock stateBlock;
  51. - (instancetype)initWithURLString:(NSString *)URLString;
  52. /**
  53. * 初始化方法
  54. *
  55. * @param URLString 下载地址
  56. * @param filePath 缓存地址 当为nil 默认缓存到cache
  57. */
  58. - (instancetype)initWithURLString:(NSString *)URLString filePath:(NSString *)filePath;
  59. @end
  60. /**
  61. * 下载进度
  62. */
  63. @interface TYDownloadProgress : NSObject
  64. // 续传大小
  65. @property (nonatomic, assign, readonly) int64_t resumeBytesWritten;
  66. // 这次写入的数量
  67. @property (nonatomic, assign, readonly) int64_t bytesWritten;
  68. // 已下载的数量
  69. @property (nonatomic, assign, readonly) int64_t totalBytesWritten;
  70. // 文件的总大小
  71. @property (nonatomic, assign, readonly) int64_t totalBytesExpectedToWrite;
  72. // 下载进度
  73. @property (nonatomic, assign, readonly) float progress;
  74. // 下载速度
  75. @property (nonatomic, assign, readonly) float speed;
  76. // 下载剩余时间
  77. @property (nonatomic, assign, readonly) int remainingTime;
  78. @end