MCDownloader.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // MCDownloader.h
  3. // MCDownloadManager
  4. //
  5. // Created by M.C on 17/4/6. (QQ:714080794 Gmail:chaoma0609@gmail.com)
  6. // Copyright © 2017年 qikeyun. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import <Foundation/Foundation.h>
  27. #import "MCDownloadReceipt.h"
  28. #import <UIKit/UIKit.h>
  29. NS_ASSUME_NONNULL_BEGIN
  30. // Use dispatch_main_async_safe instead of dispatch_async(dispatch_get_main_queue(), block)
  31. #ifndef dispatch_main_async_safe
  32. #define dispatch_main_async_safe(block)\
  33. if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {\
  34. block();\
  35. } else {\
  36. dispatch_async(dispatch_get_main_queue(), block);\
  37. }
  38. #endif
  39. FOUNDATION_EXPORT NSString * const MCDownloadCacheFolderName;
  40. FOUNDATION_EXPORT NSString * cacheFolder();
  41. extern NSString * _Nonnull const MCDownloadStartNotification;
  42. extern NSString * _Nonnull const MCDownloadStopNotification;
  43. typedef NSDictionary<NSString *, NSString *> MCHTTPHeadersDictionary;
  44. typedef NSMutableDictionary<NSString *, NSString *> MCHTTPHeadersMutableDictionary;
  45. typedef MCHTTPHeadersDictionary * _Nullable (^MCDownloaderHeadersFilterBlock)(NSURL * _Nullable url, MCHTTPHeadersDictionary * _Nullable headers);
  46. @interface MCDownloader : NSObject
  47. /**
  48. * The maximum number of concurrent downloads
  49. */
  50. @property (assign, nonatomic) NSInteger maxConcurrentDownloads;
  51. /**
  52. * Shows the current amount of downloads that still need to be downloaded
  53. */
  54. @property (readonly, nonatomic) NSUInteger currentDownloadCount;
  55. /**
  56. * The timeout value (in seconds) for the download operation. Default: 15.0.
  57. */
  58. @property (assign, nonatomic) NSTimeInterval downloadTimeout;
  59. /**
  60. Defines the order prioritization of incoming download requests being inserted into the queue. `MCDownloadPrioritizationFIFO` by default.
  61. */
  62. @property (nonatomic, assign) MCDownloadPrioritization downloadPrioritizaton;
  63. /**
  64. * Singleton method, returns the shared instance
  65. *
  66. * @return global shared instance of downloader class
  67. */
  68. + (nonnull instancetype)sharedDownloader;
  69. /**
  70. * Set filter to pick headers for downloading image HTTP request.
  71. *
  72. * This block will be invoked for each downloading image request, returned
  73. * NSDictionary will be used as headers in corresponding HTTP request.
  74. */
  75. @property (nonatomic, copy, nullable) MCDownloaderHeadersFilterBlock headersFilter;
  76. /**
  77. * Creates an instance of a downloader with specified session configuration.
  78. * *Note*: `timeoutIntervalForRequest` is going to be overwritten.
  79. * @return new instance of downloader class
  80. */
  81. - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration NS_DESIGNATED_INITIALIZER;
  82. /**
  83. * Set a value for a HTTP header to be appended to each download HTTP request.
  84. *
  85. * @param value The value for the header field. Use `nil` value to remove the header.
  86. * @param field The name of the header field to set.
  87. */
  88. - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field;
  89. /**
  90. * Returns the value of the specified HTTP header field.
  91. *
  92. * @return The value associated with the header field field, or `nil` if there is no corresponding header field.
  93. */
  94. - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field;
  95. /**
  96. * Sets a subclass of `MCDownloadOperation` as the default
  97. * `NSOperation` to be used each time MCDownload constructs a request
  98. * operation to download an data.
  99. *
  100. * @param operationClass The subclass of `MCDownloadOperation` to set
  101. * as default. Passing `nil` will revert to `MCDownloadOperation`.
  102. */
  103. - (void)setOperationClass:(nullable Class)operationClass;
  104. /**
  105. Creates an `MCDownloadReceipt` with the specified request.
  106. @param url The URL for the request.
  107. @param progressBlock A block object to be executed when the download progress is updated. Note this block is called on the main queue.
  108. */
  109. - (nullable MCDownloadReceipt *)downloadDataWithURL:(nullable NSURL *)url
  110. progress:(nullable MCDownloaderProgressBlock)progressBlock
  111. completed:(nullable MCDownloaderCompletedBlock)completedBlock;
  112. - (nullable MCDownloadReceipt *)downloadReceiptForURLString:(nullable NSString *)URLString;
  113. - (void)cancel:(nullable MCDownloadReceipt *)token completed:(nullable void (^)())completed;
  114. - (void)remove:(nullable MCDownloadReceipt *)token completed:(nullable void (^)())completed;
  115. /**
  116. * Sets the download queue suspension state
  117. */
  118. - (void)setSuspended:(BOOL)suspended;
  119. /**
  120. * Cancels all download operations in the queue
  121. */
  122. - (void)cancelAllDownloads;
  123. /**
  124. Romove All files in the cache folder.
  125. @Waring:
  126. This method is synchronized methods, you should be careful when using, will delete all the data in the cache folder
  127. */
  128. - (void)removeAndClearAll;
  129. @end
  130. NS_ASSUME_NONNULL_END