0

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

MyCode:

- (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];

    NSLog(@"%@",arrayData);


}

Can you explain for me about this error, please?

Tà Truhoada
  • 584
  • 7
  • 23

10 Answers10

3

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.

erenkabakci
  • 442
  • 4
  • 15
2

Your issue is related to App Transport Security, where HTTP is being blocked by iOS 9.

You need to edit your plist file with below:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

Get more information here: Configuring App Transport Security Exceptions

As per Apple's documents: App Transport Security Technote

You can specify exceptions to the default behavior in the Info.plist file in your app or extension. Use the keys in the property list for specific exceptions or to turn off App Transport Security. Table 1-1 shows the keys and their types, and uses indentation to indicate structure.

enter image description here

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
2

Its a iOS 9 Problem. Just Add Key in Info Plist.

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

Then its work perfectly

HariKrishnan.P
  • 1,204
  • 13
  • 23
  • add the Key in Plist after that error will not come. if url is HTTP so apple cannot allow to access,because we will get the nill exception. – HariKrishnan.P Oct 12 '15 at 10:06
2

My advice is do not try to save image as data or image object in array .You better save it as url or NSString because this can case problem later incase of huge data later. Just when you need to display it use

   UIImageView *imgDisplay; // alloc it or take how you want to take it

 // UIImageView *imgDisplay=[[UIImageView alloc] init];

 // i is the index of your array  or if static use the number like 1,2, etc

   imgDisplay.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayData objectAt Index:[i]]]]];

And so far your app got crashed because your are using http is not believed to be secured .So, if you want to allow it you have to go to your info.plist and add the the following :

   <key>NSAppTransportSecurity</key>
   <dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
 </dict>

Right click your info.plist and open it as Source code and add the above line. Then you can fetch the image which was nil before and app got crashed.Because array cannot add nil object. Hope this might help you. If you face any issue you can report.

sandeep nag
  • 148
  • 8
1

It's because Apple has introduced App Transport Policy in iOS 9. We should use secured connections.

Second URL is of "http" type. So it's showing error.

You can override this behavior by mentioning Key in info.plist. Refer this link - https://stackoverflow.com/a/32754976/1423703

Community
  • 1
  • 1
Alok Rao
  • 162
  • 1
  • 12
1

Check your console log. it says Transport security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

App Transport security is available on iOS 9.0. You need to add NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity in your plist file.

Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
1

Actually you are facing this issue related to Network Transport security, its recently introduced in iOS 9. you need to add below entry in your plist.

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
Maksud Ali
  • 121
  • 4
1

Use this code-

 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];


    NSURL *url1 = [NSURL URLWithString:stringURL2];

    NSData* imageData1 = [[NSData alloc]initWithContentsOfURL:url1];

    UIImage* image1 = [[UIImage alloc] initWithData:imageData1];

    NSMutableArray *arrayData = [[NSMutableArray alloc] initWithObjects:image,image1, nil];

    NSLog(@"%@",arrayData);

Then add

  1. Add a NSAppTransportSecurity : Dictionary.
  2. Add Subkey named " NSAllowsArbitraryLoads " as Boolean : YES

enter image description here

Now, Clean your project and run.

Avinash651
  • 1,399
  • 11
  • 17
0

I think that your facing an iOS 9 problem; All requests must be HTTPS.

You should add the NSAppTransportSecurity in your plist and set it to NSAllowsArbitraryLoads, as a 'temporary' solution.

dosdos
  • 1,519
  • 1
  • 10
  • 14
0

It will solve you crash;

UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
    NSArray *arrayData = @[@"1", @"2", image];
}
Jamil
  • 2,977
  • 1
  • 13
  • 23