NSURLConnection problem !!!

Discussion in 'iOS Programming' started by WSD, Jan 6, 2010.

  1. WSD macrumors newbie

    Joined:
    Jan 6, 2010
    #1
    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What's the URL you're trying to connect to? Does it contain spaces? Are you encoding it correctly into URLstr?
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I believe the adding of the CODE tags has affected it. Because it was correct before.
     
  5. WSD thread starter macrumors newbie

    Joined:
    Jan 6, 2010
    #5
    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?
     
  6. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #6
    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.
     

Share This Page