Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

WSD

macrumors newbie
Original poster
Jan 6, 2010
3
0
I'm trying to connect to a website(server) and get information. I use a URL string that works perfectly when I copy it to the Safari browser but when I try to perform the same task in the code, nothing happens.
I use the standard asynch NSURLConnection procedure:

Code:
NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:URLstr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0] autorelease];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
// Create the NSMutableData that will hold
// the received data
self.responseData=[[NSMutableData data] retain];
} 
else {
NSLog(@"Connection Failure!");
self.responseData = nil; 
}

I also added connection delegate methods such as:

- (void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response {
[responseData setLength:0];
}

- (void)connectionNSURLConnection *)connection didReceiveDataNSData *)data {
[self.responseData appendData:data];
}

- (void)connectionNSURLConnection *)connection didFailWithErrorNSError *)error {
[responseData release];
[connection release];
// Show error message
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
When debugging, I can see that the request object and the connection objects are not nil (i.e. there is no connection failure) but the delegate methods are never called (I put a break point in each of them). Is there something I forgot to add? should the class executing the connection inherit from a special class(other than NSObject)? should I import a class or framework apart from: #import <Foundation/Foundation.h>.
Why is there no call for the connection delegate methods?
Any ideas?
Thanx in advance,
WSD
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Is this exactly how you have it in your file or is there some issue with pasting it into the forum

Code:
- (void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response

If that's exactly it then it's not correct.
 

WSD

macrumors newbie
Original poster
Jan 6, 2010
3
0
To clear things up

I believe that the problem cannot be the URLstr that I use(and no, there r no spaces. It works fine when request is from browser itself). I tried using @"http://www.apple.com/" and it didnt work either. by "didnt work" I mean - the delegate functions were not called. In this case, at least the connectionDidFinishLoading method should have been called, right?
What I was thinking o doing is actually starting a new application, just to check the connection. I'll try writing all the code in the application delegate in the applicationDidFinishLaunching method. If it doesnt work either, it means that something is wrong with my xcode version, right?
 

drf1229

macrumors regular
Jun 22, 2009
237
0
In this case, at least the connectionDidFinishLoading method should have been called, right?
You didn't include the connectionDidFinishLoading method in your post. If you say it "doesn't work" and you don't get an error message from one of the other methods could you have made a typo in one of the methods? Did you write the delegate functions yourself or did you copy them from apple's guide? I recently started using NSURLConnection and I copied most of the delegate functions from the guide and changed them to my likings. It works just fine for me.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.