I’m using ASIHTTPRequest to perform an asynchronous URL request using a method named submitUserCredentials from my iOS application. The request is being sent just fine, but the problem is that when the response is returned from the server, ASIHTTPRequest is set up so that another method, requestFinished, receives and returns it.
When submitUserCredentials is the method that I’m running from my view controller in the first place, how do I get hold of the server response that requestFinished returns?
You set up your ViewController to conform to the ASIHTTPRequestDelegate protocol:
Then, you implement the
method in your ViewController’s implementation. Now when the request is finished the requestFinished: method in your ViewController will be called. If you need to pass information along with the request and have it be available in the requestFinished: method, you can set the ASIHTTPRequest’s userInfo property to whatever you like, and it will be available in the request parameter in requestFinished:
Response to comment:
If you are willing to block your thread until the response is received, you can use the startSynchronous method (code from the ASI site http://allseeing-i.com/ASIHTTPRequest/How-to-use):
This way your submitUserCredentials method would sit and wait while the server responds, but it would return the server response to that method. If blocking your UI while waiting a response won’t work for you (and it probably won’t) you can spin up a new thread to run your submitUserCredentials method and then call startSynchronous from there: