Last active
November 10, 2016 07:25
-
-
Save petermolnar-dev/769af6d3852595ec4176811c12f80260 to your computer and use it in GitHub Desktop.
PMODownloader.m - With Notification Center
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 | |
- (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