本文介绍了dataWithContentsOfURL返回NSCocoaErrorDomain Code = 256通过蜂窝,但不是wifi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的奇怪的问题我有:dataWithContentsOfURL已经开始返回错误代码256通过蜂窝,但不是通过wifi。

Really odd problem I'm having: dataWithContentsOfURL has begun returning an error code 256 over cellular, but not over wifi.

我确实有一个蜂窝数据连接,并且它正常工作,所以它不是一个问题我的蜂窝连接。加上代码在wifi上正常工作,因此基本代码不是问题。有问题的代码是:

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 表示:

只是它告诉我们没有

一般, NSData dataWithContentsOfURL 只能用于访问本地文件资源。

However in general, NSData's dataWithContentsOfURL should only be use to access local file resources.

您可以尝试改进代码并使用更好的下载数据的方式。它可能解决您遇到的问题。而不是使用 dataWithContentsOfURL ,您可以使用 NSURLConnection 的类方法,如:

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;

基于:

这篇关于dataWithContentsOfURL返回NSCocoaErrorDomain Code = 256通过蜂窝,但不是wifi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 16:11