The Miotz The Miotz - 20 days ago 6
iOS Question

iOS Objective-c: how to set and ImageView from an URL

Evening, I'm trying to set an ImageView.image (an IBOutlet in this case) with an image downloaded from the web.

I've already ridden a lot of questions about this in stack overflow.

And I've found this common answer:

NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"];

NSData *data = [NSData dataWithContentsOfURL:url];

NSLog(@"imageData: %@", data);

UIImage *image = [UIImage imageWithData:data];

NSLog(@"image: %@", image);

self.imageView.image = image;


But of course in my case does not work. My logs are always null.

Can you please tell me which is the problem?

Answer

I believe you are trying to run it on iOS 8 +

Please add this to your info.plist

 <key>NSAppTransportSecurity</key>
   <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
   </dict>

And everything should be fine :)

WHY ??

iOS 8 onwards http is not allowed you have to use either https or add exception to your info.plist :)

Comments