Skip to content

Commit 81d3236

Browse files
committed
refining first chapter 37 examples
1 parent 4f37980 commit 81d3236

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

ch37p1088simpleHTTP/ch37p1088simpleHTTP/ViewController.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ @interface ViewController ()
88

99
@implementation ViewController
1010

11+
// more convincing if you run it on a device
12+
1113
- (IBAction) doSimpleHTTP: (id) sender {
14+
self.iv.image = nil;
1215
NSString* s = @"http://www.apeth.net/matt/images/phoenixnewest.jpg";
1316
NSURL* url = [NSURL URLWithString:s];
1417
NSURLSession* session = [NSURLSession sharedSession];
15-
NSURLSessionDataTask* task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
18+
NSURLSessionDownloadTask* task = [session downloadTaskWithURL:url completionHandler:^(NSURL *loc, NSURLResponse *response, NSError *error) {
1619
NSLog(@"%@", @"here");
1720
if (error) {
1821
NSLog(@"%@", error);
@@ -24,7 +27,8 @@ - (IBAction) doSimpleHTTP: (id) sender {
2427
NSLog(@"%@", @"oh well");
2528
return;
2629
}
27-
UIImage* im = [UIImage imageWithData:data];
30+
NSData* d = [NSData dataWithContentsOfURL:loc];
31+
UIImage* im = [UIImage imageWithData:d];
2832
dispatch_async(dispatch_get_main_queue(), ^{
2933
self.iv.image = im;
3034
NSLog(@"%@", @"done");

ch37p1089lessSimpleHTTP/ch37p1089lessSimpleHTTP/ViewController.m

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
#import "ViewController.h"
44

5-
@interface ViewController () <NSURLSessionDataDelegate>
6-
@property (nonatomic, strong) NSMutableData* data;
5+
@interface ViewController () <NSURLSessionDownloadDelegate>
6+
// @property (nonatomic, strong) NSMutableData* data;
77
@property (nonatomic, weak) IBOutlet UIImageView* iv;
88
@property (nonatomic, strong) NSURLSession* session;
9-
@property (nonatomic, strong) NSURLSessionDataTask* task;
9+
@property (nonatomic, strong) NSURLSessionDownloadTask* task;
1010
@end
1111

1212
@implementation ViewController
1313

1414
- (NSURLSession*) configureSession {
1515
NSURLSessionConfiguration* config =
1616
[NSURLSessionConfiguration ephemeralSessionConfiguration];
17+
config.allowsCellularAccess = NO;
1718
NSURLSession* session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
1819
return session;
1920
}
@@ -27,38 +28,39 @@ - (IBAction) doElaborateHTTP: (id) sender {
2728
NSString* s = @"http://www.apeth.net/matt/images/phoenixnewest.jpg";
2829
NSURL* url = [NSURL URLWithString:s];
2930
NSURLRequest* req = [NSURLRequest requestWithURL:url];
30-
NSURLSessionDataTask* task = [[self session] dataTaskWithRequest:req];
31+
NSURLSessionDownloadTask* task = [[self session] downloadTaskWithRequest:req];
3132
self.task = task;
3233
self.iv.image = nil;
3334
[task resume];
3435
}
3536

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

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

48-
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
45+
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
4946
self.task = nil;
50-
if (error) {
51-
NSLog(@"%@", error);
47+
NSHTTPURLResponse* response = (NSHTTPURLResponse*)downloadTask.response;
48+
NSInteger stat = response.statusCode;
49+
NSLog(@"status %i", stat);
50+
if (stat != 200)
5251
return;
53-
}
54-
UIImage* im = [UIImage imageWithData:self.data];
55-
self.iv.image = im;
56-
NSLog(@"%@", @"done!");
52+
NSData* d = [NSData dataWithContentsOfURL:location];
53+
UIImage* im = [UIImage imageWithData:d];
54+
dispatch_async(dispatch_get_main_queue(), ^{
55+
self.iv.image = im;
56+
NSLog(@"%@", @"done");
57+
});
5758
}
5859

5960
-(void)viewWillDisappear:(BOOL)animated {
6061
[super viewWillDisappear:animated];
6162
[self.session finishTasksAndInvalidate];
63+
self.session = nil;
6264
}
6365

6466
-(void)dealloc {

0 commit comments

Comments
 (0)