0

When running the following lines of code, I see this in the log: Error Domain=NSCocoaErrorDomain Code=256 "The file “some_page” couldn’t be opened

Here is the code:

NSError *downloadError = nil;
NSData *theJSONData = [NSData dataWithContentsOfURL:[NSURL URLWithString:s] options:NSDataReadingUncached error:&downloadError];
NSLog(@"error : %@", downloadError);

This happens only 20-30% of the time

What could be the reason? I found an answer where they say iOS doesn't support http, so I added NS app transport security. But anyway, it happens only sometimes.

Update: I just found out that this is the cause:

if(! [[fullURL substringFromIndex:fullURL.length-1] isEqualToString:@"/"])
   s = [fullURL stringByAppendingString:@"/"];

why does it not work 20% of the time with the same url when I add the "/"?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
wp42
  • 158
  • 10
  • [this might help you](http://stackoverflow.com/a/10106824/1066828) [here is whole google search](https://www.google.com.kw/search?rlz=1C5CHFA_enKW556KW556&q=NSCocoaErrorDomain+Code%3D256&oq=NSCocoaErrorDomain+Code%3D256&gs_l=serp.3..0l2j0i22i30k1l3.6948.8691.0.8939.2.2.0.0.0.0.223.409.0j1j1.2.0....0...1c.1.64.serp..0.2.408...30i10k1.FEdUSupRN9E) – Fahim Parkar Jan 04 '17 at 08:15
  • Is your URL string (s) always the same or you construct it dynamically each time ? – Bogdan Farca Jan 04 '17 at 08:40
  • @BogdanFarca It's always the same, and the same one doesn't work 20% of the times – wp42 Jan 04 '17 at 08:46
  • Are you sure the URL is always working, regardless of your app? Could you try it in a browser repeatedly and see if it's working in there ? – Bogdan Farca Jan 04 '17 at 08:56
  • @BogdanFarca yes, but see the next update please. – wp42 Jan 04 '17 at 09:03
  • Check before hand if `[NSURL URLWithString:s]` is not `nil`. – Larme Jan 04 '17 at 09:06
  • So, with the update it works 100% ? – Bogdan Farca Jan 04 '17 at 09:10
  • 1
    Basically **never ever** load data from a remote URL with the synchronous method `dataWithContentsOfURL`. Use asynchronous `URLSession` – vadian Jan 04 '17 at 09:20
  • url is not null. with this update it works 100% of the time. but the question is still relevant, I would like to find an answer. – wp42 Jan 04 '17 at 09:23
  • @wp42: Try to logs always `fullURL`, and try them in browser. Check if the one with `/` or `without` are working on web browser or with a POSTMAN-like. – Larme Jan 04 '17 at 14:30
  • @Larme of course they work – wp42 Jan 04 '17 at 16:18

0 Answers0