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