views:

513

answers:

2

Hello all.

I am attempting to use ASIHttpRequest with the iPhone to get some information from a query-string based API on my site.

Currently, I am in the planning stages of my design and I am kind of stuck on how I should proceed.

I would like to have a class, that will handle all of the requests I will need. I plan on having different sections of the iPhone application call this class and within the class, I would like to run the ASIHttpRequest code. For example:

  1. I will pop up a modal view that will login.
  2. This modal view will call a function with the user/pass to the API class which will then put together the URL and call it.
  3. Once the URL request has been processed and finished, it will go into the requestFinished method or whichever I choose as the selector.
  4. The modal view will get back the responseString and proceed as desired.

This is where I am stuck. I would like to somehow pass the responseString back to the original caller of the network url request.

Should I use a delegate method? If so, how can I call that from within the requestFinished method? I thought about this, but I am a little confused as to how I would have the requestFinished function call that delegate. I might be missing a simple way to pass that delegate as an object along with an ASIHttpRequest.

I also thought about NSNotificationCenter, but I feel that would be more work than using a delegate.

I appreciate any help given.

+1  A: 

It depends on what do you want to do with response. If you just want to show an alert message with response you can do it directly in your request delagate method. Another way is to store your responce in class with share data and post a notification to tell the listener that it has data to process. It's not difficult) And the third variant. You can create a basic class for your client-server communication with delegate and selector properties. And then just call [delegate performSelector:selector] if you need it. Then you can inherit other classes for login, checking versions, etc.

Morion
Could you possibly expand on the third option? I was trying to plan it out, but I am not seeing it the way I think I should be.
statikfx
A: 

You can subclass ASIHttpRequest and either override its - (void)requestFinished which allows you to do some processing on the background thread (and still call [super requestFinished];) or simply add an instance variable id myDelegate; with an accessor so you can access it from your -requestFinished

Roger Nolan
Would using a delegate be the best way to handle this issue?
statikfx
WIthout perfect sight, yes I think so.
Roger Nolan