views:

333

answers:

1

I'm writing a RSS feeder and using the NSXMLParser to parse an XML page for me. Everything works well.

This is the code that handles the asynchronously connection:

static NSString *feedURLString = @"http://www.example.com/data.xml";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];
feed = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

Now i'm trying to add another website to be parsed using the same code above, but i need to do different action, on a different URL

I'm implementing the delegate function:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

The problem is i can't figure out which website was called, i only got the data.

How can i figure out which URL the connection is resolved to?

For example in the :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

I can check the NSSTRING in NSURL URL that comes from the NSURLResponse but in the above function i can't.

+2  A: 

If you implement both delegate methods, shouldn't you receive the latter notification first? If so, you can associate the URL string with the NSURLConnection (which will assumedly be different for each site) in an instance variable and use it when you get the data afterward. I would generally suggest a dictionary, although you can't use the connection as a key in an NS(Mutable)Dictionary since it doesn't conform to NSCopying. You could use the URL string as the key, but that complicates lookup. Perhaps a pair of arrays?

More to the point though, why write an RSS reader from scratch? On 10.5+ you can use the PubSub.framework to do the work for you. This framework handles all sorts of weird formats and invalid XML, and can really save you a lot of time. Maybe it's a good fit for what you're trying to do?

Quinn Taylor
On 10.5+, you could use `NSMapTable` to handle the `NSURLConnection -> NSURL` mapping. On 10.0 through 10.4, you could do the same, but you'd be stuck using the clunky C API (`NSCreateMapTable` and friends) instead of an Obj-C API.Definitely give the PubSub framework a look if you can require 10.5+.
Jeremy W. Sherman
Excellent point, NSMapTable is a great alternative, and easier than diving into CFMutableDictionary. :-)
Quinn Taylor