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).
NSCocoaErrorDomain Code = 256
means:
A file system or file I/O related error whose reason is unknown.
Simply, it tells us nothing.
However in general, NSData
's dataWithContentsOfURL
should only be use to access local file resources.
Important: Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.
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 use NSURLConnection
's class methods like:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request
queue:(NSOperationQueue *)queue
completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler;
Based on: