Ios NSURLConnectionDataDelegate和NSURLConnectionDownloadDelegate实现这两个功能时工作不正常
我想实现两个代理。在实现这两个Ios NSURLConnectionDataDelegate和NSURLConnectionDownloadDelegate实现这两个功能时工作不正常,ios,iphone,objective-c,nsurlconnection,Ios,Iphone,Objective C,Nsurlconnection,我想实现两个代理。在实现这两个NSURLConnectionDataDelegate时,将调用两个方法。如果我正在删除nsurconnectionDownloadDelegate方法,则nsurconnectionDataDelegate方法工作正常。我不明白原因。下面是代码 -(void)downloadImageFromServer { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
NSURLConnectionDataDelegate
时,将调用两个方法。如果我正在删除nsurconnectionDownloadDelegate
方法,则nsurconnectionDataDelegate
方法工作正常。我不明白原因。下面是代码
-(void)downloadImageFromServer
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:/*************/-yqaDz5cGzAY/USm27B4zbOI/AAAAAAAADEA/3sgkCfGNwqw/s1600/lion_hd_wallpaper.jpg"]];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.mData = [NSMutableData data];
NSLog(@"http header fields %@", [(NSHTTPURLResponse*)response allHeaderFields]);
NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields];
long int dataLength = [[headerFields valueForKey:@"Content-Length"] intValue];
self.progress = [NSProgress progressWithTotalUnitCount:dataLength];
[_progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_mData appendData:data];
self.ImageViewer.image = [UIImage imageWithData:_mData];
}
- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes
{
NSLog(@"DOwnloading..");
}
您是否实现了连接:didFailWithError:方法?是的,实现了。这也没有被调用。关于.h文件,您是否符合NSURLConnectDelegate?这可能是一个线索(来自文档)“NSURLConnectionDownloadDelegate协议描述了应该由NSURLConnection实例的委托实现的方法,这些实例是使用报摊工具包的downloadWithDelegate:方法创建的。”@改革者使三位代表都符合要求。