NSURLConnection Delegate RedirectResponse is always nil

Discussion in 'iOS Programming' started by roeik, Dec 29, 2009.

  1. roeik macrumors member

    Dec 25, 2008
    Hi all,

    I have this problem that is driving me insane. I am using the WillSendRequest RedirectResponse delegate method of NSURLConnection to find out if I am being redirected. The problem is that it doesn't work properly. RedirectResponse for some reason is always nil, and "request" seems to be the original request instead of the request with the redirect url.

    Any idea why it is doing this?

    Please help.

    Here is the code:

    -(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
    	NSURLRequest *newRequest=request;
    	NSLog(@"Request: %@",[[request URL] absoluteURL]);
    	NSLog(@"The Request: %@",[[theRequest URL] absoluteURL]);
    	if (redirectResponse) {
    		NSLog(@"redirect requested!");
    		[connection cancel];
    		[connection release];
    		[receivedData release];
    		[updateController cancelDownloaders];
    		[updateController alertSimpleAction: @"Could not download file. \n Please check your username and  password"];
    	return newRequest;
  2. roeik thread starter macrumors member

    Dec 25, 2008

    I am surprised nobody had this problem before.

    I would take any advice about hot to top a connection when the request is being redirected to another page.
  3. robbieduncan Moderator emeritus


    Jul 24, 2002
    1) Don't bump your posts. It's amazingly rude and arrogant as well as being against the forum rules. So much so that I almost did not post point 2

    2) As per the documentation redirectResponse "May be nil in cases where this method is not being sent as a result of involving the delegate in redirect processing."
  4. roeik thread starter macrumors member

    Dec 25, 2008

    Regarding point 1, Sorry. I am not sure what you mean by bump up, but I apologize if I caused you any damage of any sort. The last thing i meant was to be arrogant or rude, and again, I am deeply sorry if I broke the forum rules. Thank you for letting me know.

    Regarding point 2, I read the documentation as well and saw this point, but I guess it was and still is vague to me. And it doesn't solve me the problem. My URLResponse is being redirected to another page, yet the redirect delegate method is being called and the redirect is nil. Why would this method get called if there is no redirect call? I don't understand.
  5. robbieduncan Moderator emeritus


    Jul 24, 2002
    I think you are misunderstanding what the redirectResponse should/will contain. In the case where you receive a redirect and the delegate has not yet been involved the redirectResponse will be nil (this is my understanding of what the documentation is saying). The request variable is what will be sent (i.e. where you have been redirected to). If you want to alter than you should copy and change it before returning.

Share This Page