Skip to content

Instantly share code, notes, and snippets.

@petermolnar-dev
Created January 6, 2017 21:26
Show Gist options
  • Save petermolnar-dev/8508d3881e07f39db3d7e74f141caf30 to your computer and use it in GitHub Desktop.
Save petermolnar-dev/8508d3881e07f39db3d7e74f141caf30 to your computer and use it in GitHub Desktop.
PMODownloader.m - With delegate
#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