Skip to content

Commit

Permalink
refining first chapter 37 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mattneub committed Nov 14, 2013
1 parent 4f37980 commit 81d3236
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
8 changes: 6 additions & 2 deletions ch37p1088simpleHTTP/ch37p1088simpleHTTP/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ @interface ViewController ()

@implementation ViewController

// more convincing if you run it on a device

- (IBAction) doSimpleHTTP: (id) sender {
self.iv.image = nil;
NSString* s = @"http://www.apeth.net/matt/images/phoenixnewest.jpg";
NSURL* url = [NSURL URLWithString:s];
NSURLSession* session = [NSURLSession sharedSession];
NSURLSessionDataTask* task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSURLSessionDownloadTask* task = [session downloadTaskWithURL:url completionHandler:^(NSURL *loc, NSURLResponse *response, NSError *error) {
NSLog(@"%@", @"here");
if (error) {
NSLog(@"%@", error);
Expand All @@ -24,7 +27,8 @@ - (IBAction) doSimpleHTTP: (id) sender {
NSLog(@"%@", @"oh well");
return;
}
UIImage* im = [UIImage imageWithData:data];
NSData* d = [NSData dataWithContentsOfURL:loc];
UIImage* im = [UIImage imageWithData:d];
dispatch_async(dispatch_get_main_queue(), ^{
self.iv.image = im;
NSLog(@"%@", @"done");
Expand Down
40 changes: 21 additions & 19 deletions ch37p1089lessSimpleHTTP/ch37p1089lessSimpleHTTP/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

#import "ViewController.h"

@interface ViewController () <NSURLSessionDataDelegate>
@property (nonatomic, strong) NSMutableData* data;
@interface ViewController () <NSURLSessionDownloadDelegate>
// @property (nonatomic, strong) NSMutableData* data;
@property (nonatomic, weak) IBOutlet UIImageView* iv;
@property (nonatomic, strong) NSURLSession* session;
@property (nonatomic, strong) NSURLSessionDataTask* task;
@property (nonatomic, strong) NSURLSessionDownloadTask* task;
@end

@implementation ViewController

- (NSURLSession*) configureSession {
NSURLSessionConfiguration* config =
[NSURLSessionConfiguration ephemeralSessionConfiguration];
config.allowsCellularAccess = NO;
NSURLSession* session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
return session;
}
Expand All @@ -27,38 +28,39 @@ - (IBAction) doElaborateHTTP: (id) sender {
NSString* s = @"http://www.apeth.net/matt/images/phoenixnewest.jpg";
NSURL* url = [NSURL URLWithString:s];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask* task = [[self session] dataTaskWithRequest:req];
NSURLSessionDownloadTask* task = [[self session] downloadTaskWithRequest:req];
self.task = task;
self.iv.image = nil;
[task resume];
}

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
self.data = [NSMutableData data];
NSInteger status = [(NSHTTPURLResponse*)response statusCode];
NSLog(@"got response: %d", status);
completionHandler(NSURLSessionResponseAllow);
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
NSLog(@"downloaded %d%%", (int)(100.0*totalBytesWritten/totalBytesExpectedToWrite));
}

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSLog(@"%@", @"got some data");
[self.data appendData:data];
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
// unused in this example
}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
self.task = nil;
if (error) {
NSLog(@"%@", error);
NSHTTPURLResponse* response = (NSHTTPURLResponse*)downloadTask.response;
NSInteger stat = response.statusCode;
NSLog(@"status %i", stat);
if (stat != 200)
return;
}
UIImage* im = [UIImage imageWithData:self.data];
self.iv.image = im;
NSLog(@"%@", @"done!");
NSData* d = [NSData dataWithContentsOfURL:location];
UIImage* im = [UIImage imageWithData:d];
dispatch_async(dispatch_get_main_queue(), ^{
self.iv.image = im;
NSLog(@"%@", @"done");
});
}

-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.session finishTasksAndInvalidate];
self.session = nil;
}

-(void)dealloc {
Expand Down

0 comments on commit 81d3236

Please sign in to comment.