开发者

How do I distinguish between NSURLConnections in delegate methods?

I'm writ开发者_开发问答ing 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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜