Created
January 6, 2017 21:26
-
-
Save petermolnar-dev/8508d3881e07f39db3d7e74f141caf30 to your computer and use it in GitHub Desktop.
PMODownloader.m - With delegate
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 | |
//1 | |
@synthesize receiver = _receiver; | |
//2 | |
#pragma mark - Public API / Protocol implementation | |
- (void)downloadDataFromURL:(NSURL *)url { | |
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 { | |
[self.receiver didDownloadedData: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