This is the standard asynchronous template loader:
In the .h file:
NSMutableData *responseData;
And the .m file:
-(void) execute { NSString *urlString = @"http://www.google.com"; responseData = [[NSMutableData data] retain]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ [responseData setLength:0]; } -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [responseData appendData:data]; } -(void) connectionDidFinishLoading:(NSURLConnection *)connection{ [connection release]; NSString *data = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease]; NSLog(@"%@", data); [responseData release]; }
This will download Google and print the content.
source share