Tà Truhoada Tà Truhoada - 1 year ago 41
iOS Question

Error: Get Image from URL

I have 2 image URLs and 2 image URLs show image when i add them to browser.
I have a error when i get image from URL and put it to array.

Case1: I use stringURL1, and OK
enter image description here

Case2: I use stringURL2, and then, as you see, it crashed! enter image description here


- (void)viewDidLoad {
[super viewDidLoad];
NSString* stringURL1 = @"https://www.wonderplugin.com/wp-content/uploads/2014/06/wordpress-lightbox-gallery.png";

NSString* stringURL2 = @"http://cdn3.vox-cdn.com/uploads/chorus_asset/file/917470/iphone-6-travel-photo-review-mann-header.0.jpg";

NSURL *url = [NSURL URLWithString:stringURL1];

NSData* imageData = [[NSData alloc]initWithContentsOfURL:url];

UIImage* image = [[UIImage alloc] initWithData:imageData];

NSArray *arrayData = @[@"1", @"2", image];



Can you explain for me about this error, please?

Answer Source

This is caused because of iOS9. iOS9 requires safe connection which is provided by https instead of http and a strong enough certificate to support it.

There are 2 ways to make this happen.

Either you need to reach an https endpoint instead of http or just modify your .plist file to bypass them.

There is a dictionary called NSAppTransportSecurity in your .plist and NSAllowsArbitraryLoads boolean under it. Changing this to YES will let you bypass them.

plist example

Keep this in mind that bypassing is a temporary solution and will cause you possible security problems.

EDIT according to comments

It is crashing because the connection never happens. Image stays nil and you try to put nil object in an array in the end which causes a crash.