Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compatibility in the podspec to watchOS, macOS and tvOS #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions TWRDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

#import "TWRDownloadManager.h"
#import "TWRDownloadObject.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif

@interface TWRDownloadManager () <NSURLSessionDelegate, NSURLSessionDownloadDelegate>

Expand Down Expand Up @@ -39,11 +41,19 @@ - (instancetype)init {
// Background session
NSURLSessionConfiguration *backgroundConfiguration = nil;

#ifdef TARGET_OS_IPHONE
#ifndef TARGET_OS_WATCH
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
} else {
#endif
#endif
backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"re.touchwa.downloadmanager"];
#ifdef TARGET_OS_IPHONE
#ifndef TARGET_OS_WATCH
}
#endif
#endif

self.backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfiguration delegate:self delegateQueue:nil];

Expand All @@ -57,7 +67,6 @@ - (instancetype)init {
- (void)downloadFileForURL:(NSString *)urlString
withName:(NSString *)fileName
inDirectoryNamed:(NSString *)directory
friendlyName:(NSString *)friendlyName
progressBlock:(void(^)(CGFloat progress))progressBlock
remainingTime:(void(^)(NSUInteger seconds))remainingTimeBlock
completionBlock:(void(^)(BOOL completed))completionBlock
Expand All @@ -66,11 +75,7 @@ - (void)downloadFileForURL:(NSString *)urlString
if (!fileName) {
fileName = [urlString lastPathComponent];
}

if (!friendlyName) {
friendlyName = fileName;
}


if (![self fileDownloadCompletedForUrl:urlString]) {
NSLog(@"File is downloading!");
} else if (![self fileExistsWithName:fileName inDirectory:directory]) {
Expand All @@ -84,7 +89,6 @@ - (void)downloadFileForURL:(NSString *)urlString
TWRDownloadObject *downloadObject = [[TWRDownloadObject alloc] initWithDownloadTask:downloadTask progressBlock:progressBlock remainingTime:remainingTimeBlock completionBlock:completionBlock];
downloadObject.startDate = [NSDate date];
downloadObject.fileName = fileName;
downloadObject.friendlyName = friendlyName;
downloadObject.directoryName = directory;
[self.downloads addEntriesFromDictionary:@{urlString:downloadObject}];
[downloadTask resume];
Expand All @@ -93,16 +97,6 @@ - (void)downloadFileForURL:(NSString *)urlString
}
}

- (void)downloadFileForURL:(NSString *)urlString
withName:(NSString *)fileName
inDirectoryNamed:(NSString *)directory
progressBlock:(void(^)(CGFloat progress))progressBlock
remainingTime:(void(^)(NSUInteger seconds))remainingTimeBlock
completionBlock:(void(^)(BOOL completed))completionBlock
enableBackgroundMode:(BOOL)backgroundMode {

}

- (void)downloadFileForURL:(NSString *)url
inDirectoryNamed:(NSString *)directory
progressBlock:(void(^)(CGFloat progress))progressBlock
Expand Down Expand Up @@ -139,11 +133,11 @@ - (void)downloadFileForURL:(NSString *)urlString
completionBlock:(void(^)(BOOL completed))completionBlock
enableBackgroundMode:(BOOL)backgroundMode {
[self downloadFileForURL:urlString
withName:fileName
inDirectoryNamed:directory
progressBlock:progressBlock
remainingTime:nil
completionBlock:completionBlock
withName:fileName
inDirectoryNamed:directory
progressBlock:progressBlock
remainingTime:nil
completionBlock:completionBlock
enableBackgroundMode:backgroundMode];
}

Expand Down Expand Up @@ -280,10 +274,14 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
[self.downloads removeObjectForKey:fileIdentifier];

dispatch_async(dispatch_get_main_queue(), ^{
#ifdef TARGET_OS_IPHONE
#ifndef TARGET_OS_WATCH
// Show a local notification when download is over.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = [NSString stringWithFormat:@"%@ has been downloaded", download.friendlyName];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
#endif
#endif
});
}

Expand Down Expand Up @@ -486,10 +484,14 @@ - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
// Call the completion handler to tell the system that there are no other background transfers.
completionHandler();

#ifdef TARGET_OS_IPHONE
#ifndef TARGET_OS_WATCH
// Show a local notification when all downloads are over.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"All files have been downloaded!";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
#endif
#endif
}];

// Make nil the backgroundTransferCompletionHandler.
Expand Down
6 changes: 5 additions & 1 deletion TWRDownloadManager.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ Pod::Spec.new do |s|
:tag => "1.2.0"
}

s.platform = :ios, '7.0'
s.ios.deployment_target = '7.0'
s.osx.deployment_target = '10.8'
s.watchos.deployment_target = '3.0'
s.tvos.deployment_target = '10.0'

s.source_files = '*.{h,m}'
s.requires_arc = true

Expand Down