Created
January 26, 2017 21:24
-
-
Save petermolnar-dev/b744af05d1099a74a5302110721e51a1 to your computer and use it in GitHub Desktop.
PMODownloader.m - Block based solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PMODownloader.h" | |
#import "PMODownloadNotifications.h" | |
@implementation PMODownloader | |
#pragma mark - Public API / Protocol implementation | |
- (void)downloadDataFromURL:(nonnull NSURL *)url completionHander:(void(^_Nonnull)(NSData * _Nullable downloadedData))callback { | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
NSURLSession *session = [NSURLSession sharedSession]; | |
NSURLSessionDataTask *task = [session dataTaskWithRequest:request | |
completionHandler: | |
^(NSData *data, NSURLResponse *response, NSError *error) { | |
if (error) { | |
[self notifyObserverDownloadFailure]; | |
} else { | |
//2 | |
callback(data); | |
} | |
}]; | |
[task resume]; | |
} | |
#pragma mark - Notifications | |
- (void)notifyObserverDownloadFailure { | |
[[NSNotificationCenter defaultCenter] postNotificationName:PMODownloadFailed | |
object:self | |
userInfo:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment