0

I am migrating my deprecated iOS9 code to iOS10, specifically this:

@interface IconDownloader : NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate>{
    NSMutableData *receivedData;
}
@property(nonatomic,strong)CapabilityType *capability;
@property(nonatomic,strong)CapabilitiesItemView *item;
-(void)downloadIcon:(CapabilityType *)capability forItem:(CapabilitiesItemView *)item;
@end

@implementation IconDownloader
.
.
.
.
NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
    [con start];

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"DEPRECATED - didReceiveData"); 
    if(!receivedData){
        receivedData=[[NSMutableData alloc] initWithData:data];
    }else
        [receivedData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"DEPRECATED - connectionDidFinishLoading"); 
    NSLog(@"NSURLConnection - COMPLETE, dataLength=%lu",(unsigned long)receivedData.length);
    NSLog(@"DataIOS7+: %@",receivedData);
    if(receivedData.length>0){
        UIImage *image=[UIImage imageWithData:receivedData];
        if(image){
            self.capability.icon=image;
            self.item.iconImageView.image=image;
        }
    }

This deprecated, but reliable code works perfectly. When I try to do an adaptation for iOS10+, the image variable always is initialized as null, and my imageViews look blank:

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:req
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
                                      NSLog(@"NSURLSessionDataTask - COMPLETE, error=%@",error);
                                      if(!error){
                                          NSLog(@"NSURLSessionDataTask - COMPLETE, dataLength=%lu",(unsigned long)data.length);
                                          NSLog(@"DataIOS10: %@",data);
                                          if(data.length>0){
                                              NSData *dataIOS10 = [[NSData alloc] initWithBase64EncodedString:[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] options:NSDataBase64DecodingIgnoreUnknownCharacters];
                                              UIImage *image=[UIImage imageWithData:dataIOS10];

                                              dispatch_async(dispatch_get_main_queue(), ^{
                                              if(image){
                                                  NSLog(@"NSURLSessionDataTask - COMPLETE, IMAGE OK");
                                                  self.capability.icon=image;
                                                  self.item.iconImageView.image=image;
                                              }
                                              else{
                                                  NSLog(@"NSURLSessionDataTask - COMPLETE, IMAGE ERROR");
                                                  self.capability.icon=image;//Lets try shoving that data there, just in case LOL
                                                  self.item.iconImageView.image=image;
                                              }
                                              });
                                          }
                                      }
                                  }];
[dataTask resume];

EXTRA, the request appears to arrive fine:

NSURLSessionDataTask - COMPLETE, response=<NSHTTPURLResponse: 0x15e450f0> { URL: https://abc.SOMESERVER.net/prod/wcid/iphone/icon_fun.png } { status code: 200, headers {
    "Accept-Ranges" = bytes;
    Connection = "keep-alive";
    "Content-Length" = 6416;
    "Content-Type" = "image/png";
    Date = "Tue, 31 Jan 2017 13:11:05 GMT";
    Etag = "\"e554f6246076dc4eddf589f9552608b5\"";
    "Last-Modified" = "Mon, 28 Apr 2014 13:30:15 GMT";
    Server = AmazonS3;
    Via = "1.1 dc81da318a4ae20e51ccfd9463219596.cloudfront.net (CloudFront)";
    "X-Amz-Cf-Id" = "xlj4_3IUrjctZLrGlegXoZbrmTYqd_ahcGzG3K2glG3m6keedPk6mQ==";
    "X-Cache" = "Miss from cloudfront";
    "x-amz-meta-cb-modifiedtime" = "Thu, 31 Oct 2013 15:26:16 GMT";
} }

The image will always be null, despite always having non zero byte size, and with or without that fancy Base64 encoding... it always fails. I even tried running the code on the main thread. Any ideas why?

Josh
  • 6,251
  • 2
  • 46
  • 73
  • did you get what is the issue – Emel Elias Feb 06 '17 at 12:20
  • Nope @EmelElias I don't have more time to waste on this, so I used your synchronous call because I only want to load 5 icons. Not the best solution, but I am short of time. – Josh Feb 06 '17 at 12:55

1 Answers1

0

Good starting point would be to check the synchronous call is working i.e

NSData *imgData = [[NSData alloc] initWithContentsOfURL:url]; UIImage *image=[UIImage imageWithData:imgData];

If data you not able to image then there is something wrong with your request.

Emel Elias
  • 572
  • 7
  • 18