Skip to content

Commit

Permalink
Merge pull request #28 from ML-Works/feature/configuration-id
Browse files Browse the repository at this point in the history
Add costructor for configuring NSURLSession identifier
  • Loading branch information
Heikowi authored Jun 16, 2017
2 parents 2a97f06 + 96f7efa commit 1251894
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 14 additions & 1 deletion HWIFileDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -62,13 +66,22 @@ typedef void (^HWIFileDownloaderPauseResumeDataBlock)(NSData * _Nullable aResume
*/
- (nullable instancetype)initWithDelegate:(nonnull NSObject<HWIFileDownloadDelegate>*)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<HWIFileDownloadDelegate>*)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<HWIFileDownloadDelegate>*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount;
- (nullable instancetype)initWithDelegate:(nonnull NSObject<HWIFileDownloadDelegate>*)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:")));

Expand Down
14 changes: 10 additions & 4 deletions HWIFileDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

@interface HWIFileDownloader()<NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSURLConnectionDelegate>

@property (nonatomic, copy, nonnull) NSString *backgroundSessionIdentifier;
@property (nonatomic, strong, nullable) NSURLSession *backgroundSession;
@property (nonatomic, strong, nonnull) NSMutableDictionary<NSNumber *, HWIFileDownloadItem *> *activeDownloadsDictionary;
@property (nonatomic, strong, nonnull) NSMutableArray<NSDictionary <NSString *, NSObject *> *> *waitingDownloadsArray;
Expand All @@ -63,12 +64,18 @@ - (nullable instancetype)initWithDelegate:(nonnull NSObject<HWIFileDownloadDeleg
return [self initWithDelegate:aDelegate maxConcurrentDownloads:-1];
}


- (nullable instancetype)initWithDelegate:(nonnull NSObject<HWIFileDownloadDelegate>*)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<HWIFileDownloadDelegate>*)aDelegate maxConcurrentDownloads:(NSInteger)aMaxConcurrentFileDownloadsCount backgroundSessionIdentifier:(nonnull NSString *)aBackgroundSessionIdentifier
{
self = [super init];
if (self)
{
self.backgroundSessionIdentifier = aBackgroundSessionIdentifier;
self.maxConcurrentFileDownloadsCount = -1;
if (aMaxConcurrentFileDownloadsCount > 0)
{
Expand All @@ -82,17 +89,16 @@ - (nullable instancetype)initWithDelegate:(nonnull NSObject<HWIFileDownloadDeleg

if (floor(NSFoundationVersionNumber) > 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:)])
Expand Down

0 comments on commit 1251894

Please sign in to comment.