I’m trying to pull some data from a webpage. I have it partially working, but it is only pulling a snippet of HTML from this webpage rather than all of it. Here is my implementation code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://waterdata.usgs.gov/ga/nwis/uv?cb_72036=on&cb_00062=on&format=gif_default&period=1&site_no=02334400"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
// Connect
label.text = @"Connecting...";
} else {
// Error
}
}
- (void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
-(void)connectionDidFinishLoading: (NSURLConnection *)connection {
label.text = response;
connection = nil;
}
What do I need to do to get the whole page?
didReceiveData:pulls in chunks to ensure not only the fastest downloading, but the potential to download massive files without having a 2GB NSData object and crashing the application for taking up too much memory.What you want to do is create an
NSMutableDatainstance variable.Allocate and initialize it either in the init method of your view controller or when the connection starts.
In
didReceiveData:you are going to simply append the data to yourNSMutableDataThen in
connectionDidFinishLoading:You can actually remove response as an instance variable in this case (unless you need it later).