I’m making an NSURLConnection to my server but responseData ends up being null in connectionDidFinishLoading and I can’t figure out why. Here’s the code for the connection:
NSMutableData *responseData;
- (void)myFunction:(id)sender {
NSString *url = @"http://www.example.com/";
NSURL *URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
// NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
} // end connectionDidFinishLoading
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection error: %@",[error description]);
}
Typical! Just as I finished posting the question the answer dawned on me. I never initialized
responseData. So inviewDidLoadI added the line below: