From 96f7efa0bb5a7241a3f134c1ace452ab7b8a0793 Mon Sep 17 00:00:00 2001 From: Andrew Podkovyrin Date: Wed, 14 Jun 2017 15:12:01 +0300 Subject: [PATCH] Add costructor for configuring NSURLSession identifier --- HWIFileDownloader.h | 15 ++++++++++++++- HWIFileDownloader.m | 14 ++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/HWIFileDownloader.h b/HWIFileDownloader.h index 4ef6558..168d4b8 100644 --- a/HWIFileDownloader.h +++ b/HWIFileDownloader.h @@ -51,6 +51,10 @@ typedef void (^HWIFileDownloaderPauseResumeDataBlock)(NSData * _Nullable aResume */ @interface HWIFileDownloader : NSObject +/** + NSURLSession's configuration identifier + */ +@property (readonly, nonatomic, copy, nonnull) NSString *backgroundSessionIdentifier; #pragma mark - Initialization @@ -62,13 +66,22 @@ typedef void (^HWIFileDownloaderPauseResumeDataBlock)(NSData * _Nullable aResume */ - (nullable instancetype)initWithDelegate:(nonnull NSObject*)aDelegate; +/** + Secondary initializer. + @param aDelegate Delegate for salient download events. + @param aMaxConcurrentFileDownloadsCount Maximum number of concurrent downloads. Default: no limit. + @return HWIFileDownloader. + */ +- (nullable instancetype)initWithDelegate:(nonnull NSObject*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount; + /** Designated initializer. @param aDelegate Delegate for salient download events. @param aMaxConcurrentFileDownloadsCount Maximum number of concurrent downloads. Default: no limit. + @param aBackgroundSessionIdentifier NSURLSession's configuration identifier @return HWIFileDownloader. */ -- (nullable HWIFileDownloader*)initWithDelegate:(nonnull NSObject*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount; +- (nullable instancetype)initWithDelegate:(nonnull NSObject*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount backgroundSessionIdentifier:(nonnull NSString *)aBackgroundSessionIdentifier; - (nullable HWIFileDownloader*)init __attribute__((unavailable("use initWithDelegate:maxConcurrentDownloads: or initWithDelegate:"))); + (nullable HWIFileDownloader*)new __attribute__((unavailable("use initWithDelegate:maxConcurrentDownloads: or initWithDelegate:"))); diff --git a/HWIFileDownloader.m b/HWIFileDownloader.m index 5881fae..0648b70 100644 --- a/HWIFileDownloader.m +++ b/HWIFileDownloader.m @@ -39,6 +39,7 @@ @interface HWIFileDownloader() +@property (nonatomic, copy, nonnull) NSString *backgroundSessionIdentifier; @property (nonatomic, strong, nullable) NSURLSession *backgroundSession; @property (nonatomic, strong, nonnull) NSMutableDictionary *activeDownloadsDictionary; @property (nonatomic, strong, nonnull) NSMutableArray *> *waitingDownloadsArray; @@ -63,12 +64,18 @@ - (nullable instancetype)initWithDelegate:(nonnull NSObject*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount +{ + NSString *aBackgroundDownloadSessionIdentifier = [NSString stringWithFormat:@"%@.HWIFileDownload", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]]; + return [self initWithDelegate:aDelegate maxConcurrentDownloads:aMaxConcurrentFileDownloadsCount backgroundSessionIdentifier:aBackgroundDownloadSessionIdentifier]; +} + +- (nullable instancetype)initWithDelegate:(nonnull NSObject*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount backgroundSessionIdentifier:(nonnull NSString *)aBackgroundSessionIdentifier { self = [super init]; if (self) { + self.backgroundSessionIdentifier = aBackgroundSessionIdentifier; self.maxConcurrentFileDownloadsCount = -1; if (aMaxConcurrentFileDownloadsCount > 0) { @@ -82,17 +89,16 @@ - (nullable instancetype)initWithDelegate:(nonnull NSObject NSFoundationVersionNumber_iOS_6_1) { - NSString *aBackgroundDownloadSessionIdentifier = [NSString stringWithFormat:@"%@.HWIFileDownload", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]]; NSURLSessionConfiguration *aBackgroundSessionConfiguration = nil; if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) { - aBackgroundSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:aBackgroundDownloadSessionIdentifier]; + aBackgroundSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:self.backgroundSessionIdentifier]; } else { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" - aBackgroundSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:aBackgroundDownloadSessionIdentifier]; + aBackgroundSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:self.backgroundSessionIdentifier]; #pragma GCC diagnostic pop } if ([self.fileDownloadDelegate respondsToSelector:@selector(customizeBackgroundSessionConfiguration:)])