![](/img/trans.png)
[英]How to detect if NSURLConnection sendAsynchronousRequest is finished
[英]NSURLConnection sendAsynchronousRequest - communicating with HTTPS
我正在使用iPhone應用程序,該應用程序從URL提取圖像。 我使用的是“ NSData dataWithContentsOfUrl],並且工作正常。 但是隨后,您可能已經猜到了,請求是同步的。
我希望請求是異步的。 因此,我嘗試使用NSURLConnection的sendAsynchronousRequest()調用。 但這會在方法'didFailWithError'中返回以下錯誤:
Error Domain = 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方法(尚未實現;僅進行測試以首先查看其是否有效)...
-(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];
然后在Delegate方法中處理數據。 此前,它試圖在單獨的線程中運行。 這工作正常並且是異步的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.