NSURLConnectionDelegate connection:didReceiveData

2020-03-24 03:55发布

问题:

I need some help regarding the NSURLConnectionDelegate method.

- (void)startDownload {
NSString *URLString = [NSString stringWithFormat:appRecord.imageURLString]; 
NSURL *url = [NSURL URLWithString:URLString];   
NSURLRequest *request = [NSURLRequest requestWithURL:url];

imageConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];  
if(imageConnection) {
    activeDownload = [NSMutableData data];
}
}

I am using this method to initiate the NSURLConnection, but the

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

is not calling.. Need Help

Thanks in advance, Shibin

回答1:

No single answer but: 1) Put some NSLogs in to display the URL and then validate that it is generated correctly and does return data

2) Check that you have properly declared that you conform to the NSURLConnectionDelegate protocol in the .h

3) Are you threading or messing with the runloops ? " Messages to the delegate will be sent on the thread that calls this method. By default, for the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode."



回答2:

Sorry but do you do the start in your code ? I don't see it in your extract. There should be a

 [imageConnection start]

somewhere in your code to trigger the start of the connection and get your delegate called asynchronously.