Really odd problem I'm having: dataWithContentsOfURL has begun returning an error code 256 over cellular, but not over wifi.
The operation couldn’t be completed. (Cocoa error 256.)
I do indeed have a cellular data connection, and it is functioning, so it is not a problem with my cellular connection. Plus the code works fine on wifi, so the basic code is not the issue. The code in question is:
dispatch_queue_t queue = dispatch_queue_create("com.appName.FetchImage", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
...
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"URL: %@", url);
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSError *error = [[NSError alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (error) NSLog(@"Error loading data: %@", error);
UIImage *image = [UIImage imageWithData:imgData];
...
});
Any thoughts? I'm at a loss as to why this might be happening. It also occurs with the vanilla dataWithContentsOfURL (as opposed to with options).
In my case, the issue was a bad network connection on my mobile 4G. It was not xcode-specific, it also existed in regular safari browsing. I switched to a different network and that fixed the problem. The "hint" was that the method did not return immediately - it timed out after about a minute.
NSCocoaErrorDomain Code = 256
means:Simply, it tells us nothing.
However in general,
NSData
'sdataWithContentsOfURL
should only be use to access local file resources.You can try improving your code and use a better way of downloading data. It might fix the issue you are experiencing. Instead of using
dataWithContentsOfURL
, you can useNSURLConnection
's class methods like:Based on:
Using NSURLConnection
NSData Class Reference