NSURLConnection sendAsynchronousRequest – 与HTTPS通信

我正在从一个URL获取图像的iPhone应用程序。 我正在使用'NSData dataWithContentsOfUrl]它工作正常。 但是,正如你可能猜到的那样,这个请求是同步的。

我希望这个请求是asynchronous的。 所以,我尝试使用NSURLConnection的sendAsynchronousRequest()调用。 但是这会在方法'didFailWithError'中返回以下错误:

错误域= kCFErrorDomainCFNetwork代码= 310“与安全Web代理服务器(HTTPS)通信时出现问题。

有人可以帮忙吗?

这是我的NSURLConnection代码片段:

NSURL *url = [NSURL URLWithString:notePicUrl]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0]; //NSOperationQueue *queue = [[NSOperationQueue alloc] init]; NSURLConnection* _connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO]; self.port = [NSPort port]; self.runLoop = [NSRunLoop currentRunLoop]; [self.runLoop addPort:self.port forMode:NSDefaultRunLoopMode]; [_connection scheduleInRunLoop:self.runLoop forMode:NSDefaultRunLoopMode]; [_connection start]; while (self.finished != YES ) { [self.runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 0.1]]; } [self.runLoop removePort:[self port] forMode:NSDefaultRunLoopMode]; [_connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 

和NSURLConnectionDelegate方法(尚未实现,只是testing,看看它是否工作)…

 -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response { NSLog(@"didReceiveResponse"); //_data = [[NSMutableData alloc] init]; // _data being an ivar } -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data { NSLog(@"didReceiveData"); //[_data appendData:data]; } -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error { NSLog(@"didFailWithError %@", [error description]); // Handle the error properly } -(void)connectionDidFinishLoading:(NSURLConnection*)connection { NSLog(@"connectionDidFinishLoading"); //[self handleDownloadedData]; // Deal with the data } 

我只是解决这个问题。 我尝试使用简单的代码行:

 [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES]; 

然后在委托方法中处理数据。 早些时候,它试图在一个单独的线程运行。 这工作得很好,是asynchronous的。