NSURLConnection in its own thread
NSString* url = @"http://your.url/here"; NSURL* nsurl = [NSURL URLWithString:url]; NSMutableURLRequest* urlReq = [[NSMutableURLRequest alloc] initWithURL:nsurl]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];
My class contains all the necessary delegate methods like :
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; - (void) connectionDidFinishLoading:(NSURLConnection *)connection;
But once the NSURLConnection is launched in a seperate thread, the delegate methods do not get triggered anymore. This probably because the thread is finished before your class actually executed all it’s code. After you initialized the NSURLConnection object, you actually need to put the class in an infinite loop to make sure the class is still alive when the delegate methods are hit. For example:
NSString* url = @"http://your.url/here";
NSURL* nsurl = [NSURL URLWithString:url];
NSMutableURLRequest* urlReq = [[NSMutableURLRequest alloc] initWithURL:nsurl];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
finished = TRUE;
}
‘finished’ is a bool variable, which declare in your .h file. Hope this helps some people out, because this bugged for a while…..
