0

I would like to know if there is a way to get the HTTP response for a NSURLConnection without making use of the delegate provided by Apple.

For example if I setup a connection in the following manner:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"player.UpdateProfile" forHTTPHeaderField:@"player-profile"];
[self addHttpHeaders:request];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately: YES];

Now here instead of delegating to self and parsing the response in the didReceiveResponse method, can I create a custom delegate where I can parse the response? I don't need a complete tutorial on how to do it but a nudge in the right direction would be appreciated. Thanks

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
lost found
  • 329
  • 1
  • 3
  • 12
  • 4
    Yes, you can set the delegate to any object you wish, as long as that object conforms to the protocol defined by the delegate. – danielbeard Aug 22 '13 at 16:05
  • Look into AFNetworking (https://github.com/AFNetworking/AFNetworking) on GitHUB. It is a pretty incredible library that encapsulates this functionality and makes it really easy to use. – MikeS Aug 22 '13 at 16:28
  • What do you mean by "custom delegate"? Please explain. – Marcus Adams Aug 22 '13 at 16:53

1 Answers1

0

NSURLConnection is probably easier to use these days with:

[NSURLConnection sendAsynchronousRequest: urlRequest
                                   queue: [NSOperationQueue mainQueue]
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error)
{
}

This avoids having to create any delegate object at all. Be warned: Its very difficult to cancel this sort of request, so make sure you put safeguards in the response handler.

David Doyle
  • 1,716
  • 10
  • 23