dataWithContentsOfURL returns NSCocoaErrorDomain Code=256 over cellular, but not wifi

陌路散爱 提交于 2019-11-30 16:31:59

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:

Using NSURLConnection

NSData Class Reference

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!