Problems getting NSURLConnection to work
Problems getting NSURLConnection to work
- Subject: Problems getting NSURLConnection to work
- From: Matthew Delves <email@hidden>
- Date: Sat, 9 Feb 2008 16:19:55 +1100
Greetings and Salutations,
I've setup a class as a delegate for NSURLConnection, though when I
create the NSURLConnection and provide it a delegate (self), the
delegate methods don't get called. I know they don't get called as
I've put in NSLog() statements and these statements don't get displayed.
Is there something I'm missing?
Follows is the code from the class implementation file:
Thanks,
Matthew Delves
----------------
// Instance methods
- (id) initWithUrl:(NSURL *)aUrl
{
/*
NSURLConnection why won't you work?
I follow your documentation, why won't you work?
I remove garbage collection, why won't you work?
I change the thread you run on, why won't you work?
NSURLConnection why won't you work?
*/
url = [aUrl copy]; // copy the url
[url retain];
NSLog(@"%@", url);
responseString = nil;
responseCode = 0.0;
document = nil;
urlConnection = nil;
urlRequest = nil;
urlResponse = nil;
urlData = nil;
urlRequest = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[urlRequest retain];
[urlRequest setMainDocumentURL:url];
[urlRequest setURL:url];
if([NSURLConnection canHandleRequest:urlRequest])
urlConnection = [NSURLConnection connectionWithRequest:urlRequest
delegate:self];
if(urlConnection) {
[urlConnection retain];
urlData = [[NSMutableData alloc] init];
[urlConnection start];
[urlData retain];
} else {
finishedDownload = YES;
failedDownload = YES;
NSLog(@"urlConnection has become a pointer to nil");
}
failedDownload = NO;
finishedDownload = NO;
downloading = NO;
document = nil;
NSLog(@"end of fetcher init");
if(!urlConnection)
NSLog(@"blah!");
return self;
}
- (void) dealloc
{
[urlConnection release];
urlConnection = nil;
[urlRequest release];
urlRequest = nil;
[urlResponse release];
urlResponse = nil;
[responseString release];
responseString = nil;
[document release];
document = nil;
[urlData release];
urlData = nil;
[url release];
url = nil;
[super dealloc];
}
// NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection
didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge
{
NSLog(@"cancel of authentication request");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:
(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
failedDownload = YES;
downloading = NO;
[urlRequest release];
[urlResponse release];
[urlConnection release];
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge
{
NSLog(@"Authentication request received");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data
{
NSLog(@"received data");
if(!urlData){
urlData = [[NSMutableData alloc] init];
[urlData setLength:0];
}
[urlData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:
(NSURLResponse *)response
{
NSLog(@"Response received");
downloading = YES;
[urlData setLength:0];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSCachedURLResponse *newCachedResponse=cachedResponse;
NSDictionary *newUserInfo;
newUserInfo=[NSDictionary dictionaryWithObject:[NSCalendarDate date]
forKey:@"Cached Date"];
newCachedResponse=[[[NSCachedURLResponse alloc] initWithResponse:
[cachedResponse response]
data:[cachedResponse data]
userInfo:newUserInfo
storagePolicy:[cachedResponse storagePolicy]]
autorelease];
return newCachedResponse;
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)aRequest
redirectResponse:(NSURLResponse *)redirectResponse
{
NSURLRequest *newRequest = aRequest;
if (redirectResponse) {
newRequest=nil;
}
return newRequest;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
finishedDownload = YES;
downloading = NO;
NSLog(@"Finished downloading file", url);
NSError *err= [[NSError alloc] init];
document = [[NSXMLDocument alloc] initWithData:urlData
options:(NSXMLNodePreserveWhitespace|
NSXMLNodePreserveCDATA)
error:&err];
[document retain];
[urlConnection release];
[urlRequest release];
[urlResponse release];
}
---------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden