I have a hell of a time with something that seems more likely but I can't work. I am creating an iPhone that retrieves data from a web host. I am trying to establish an asynchronous connection with the host as I want to keep the device freed up during the connection. (sendSynchronousRequest freezes
phone until the request is completed.) Here is my connection code:
//temp url to see if data is returned: NSURL *theURL = [NSURL URLWithString:@"http://www.theappleblog.com/feed"]; NSURLRequest *dataRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; /* establish the connection */ theConnection = [[NSURLConnection alloc] initWithRequest:dataRequest delegate:self startImmediately:YES]; if (theConnection == nil) { NSLog(@"Connection Failure!"); self.urlData = nil; } else { self.urlData = [[NSMutableData data] retain]; }
I have all the necessary delegation methods:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response { [urlData setLength:0]; UIApplication *application = [UIApplication sharedApplication]; application.networkActivityIndicatorVisible = YES; NSLog(@"Received Response!"); }
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)incrementalData { [self.urlData appendData:incrementalData]; NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.urlData length]]; NSLog(@"resourceData length: %d", [resourceLength intValue]); NSLog(@"filesize: %d", self.urlDataSize); NSLog(@"float filesize: %f", [self.urlDataSize floatValue]); }
-(void)connectionDidFinishLoading:(NSURLConnection*)connection { NSLog(@"Connection finished loading\n"); NSLog(@"Succeeded! Received %d bytes of data",[urlData length]); _isFinished = YES; UIApplication *application = [UIApplication sharedApplication]; application.networkActivityIndicatorVisible = NO; }
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Error: %@",[error localizedDescription]); }
As you can see, I have a boat loading log messages because I wanted to see if anything was happening. My connection check
returned as TRUE, but the data is never loaded. As I said, Iām sure that I should do (or not do) something really stupid. But what? Any help would be most appreciated.
Thanks Lawrence
asynchronous objective-c iphone cocoa-touch nsurlconnection
Leachy peachy
source share