Skip to content

Instantly share code, notes, and snippets.

@petermolnar-dev
Last active November 10, 2016 07:25
Show Gist options
  • Save petermolnar-dev/769af6d3852595ec4176811c12f80260 to your computer and use it in GitHub Desktop.
Save petermolnar-dev/769af6d3852595ec4176811c12f80260 to your computer and use it in GitHub Desktop.
PMODownloader.m - With Notification Center
#import "PMODownloader.h"
#import "PMODownloadNotifications.h"
@implementation PMODownloader
#pragma mark - Public API
- (void)downloadDataFromURL:(NSURL *)url {
//1
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 notifyObserverWithProcessedData:data];
}
}];
[task resume];
}
//2
#pragma mark - Notifications
- (void)notifyObserverWithProcessedData:(NSData *)data {
NSDictionary *userInfo = @{@"downloadedData" : data };
[[NSNotificationCenter defaultCenter] postNotificationName:PMODownloadWasSuccessful
object:self
userInfo:userInfo];
}
- (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