XMLHttpRequest in Objective C - objective-c

I was hoping to capture AJAX post requests, and I have read this thread:
UIWebViewDelegate not monitoring XMLHttpRequest?>
However I was hoping there were some way to achieve this using Obj c without going through javascript. I already have a UIWebView setup along with its delegates. But since that doesn't capture XMLHttpRequest, what should I implement to achieve this and where? Thank you.

Take a look at NSURLProtocol - this should enable you to intercept the requests.
Basically you would
- create your own class derived from NSURLProtocol and register it in applicationDidFinishLaunchingWithOptions: using NSURLProtocol:registerclass:
the connection will call initWithRequest:cachedResponse:client: and then startLoading.
you would call the connection back with URLProtocol:didReceiveResponse:cacheStoragePolicy:, some number of calls to URLProtocol:didLoadData:, and finally URLProtocolDidFinishLoading:
Once you have intercepted the calls you can allow them to procede, evesdrop on them, stop them from going forward, etc.

Related

Using NSMutableURLRequest and how to manage response/failure

I am creating an iOS app that consumes web services.
I have a class that makes the connections and stores the response in a variable. It also has a status variable where 1 indicates successful connection.
I have set up an NStimer and a function to check when the connection and download is done and if it was successful.
My question is:
Is this a proper way to manage the connection and its outcome?
any suggestions?
Here is the programming guide from Apple Developer website and it describes how to use NSURLConnection delegate. You can manage the received data in connectionDidFinishLoading: method. Notice that using these delegate methods will load data asynchronously. If you want to handle data synchronously, please try sendSynchronousRequest:returningResponse:error:, but this function should never be call in the main thread.

Tracking individual requests to Open Graph through iOS SDK

I'm building an app, essentially a very basic Facebook client using the Facebook SDK for iOS. I have implemented the FBRequestDelegate protocol, and am sending my requests to the Open Graph API in the following way
[facebook requestWithGraphPath:#"me/friends" andDelegate:self]
This works fine, and once the requested content arrives, the SDK calls the delegate method, passing in the data
- (void)request:(FBRequest *)request didLoad:(id)result
My question is, does anyone know the best practice way to use the FBRequest passed into the delegate method to explicitly identify it as coming from my original request?
If I make several requests, how can I differentiate which request the delegate call is related to? I'm using BOOL flags, and only doing one request at a time at the moment, but it's not a good way of doing it.
Any help much appreciated :)
The method requestWithGraphPath returns a FBRequest object which will be also passed to request:didLoad later. So you can simply compare the two objects and determine if they're the same.

simultaneous NSURLConnection by using unique delegates for each NSURLConnection

I am try to handle multiple NSURLConnection at the same time by using a different delegate for each connection, for each NSURLConnection I create, I create a new delegate object, but for some reason only one NSURLConnection works at a time, any NSURLConnection I try to start whilst one is already running simply do not start, my delegate does not receive any of the method calls connection:didReceiveResponse:, connection:didReceiveData:, connectionDidFinishLoading: or connection:didFailWithError:. Am I misunderstanding something about how NSURLConnection and its delegate works. Reading other posts most people seem to have a single delegate for all there connections and then use some kind of dictionary to get the right object to handle the right connection. Is that the way you have to do it.
What you are describing should work fine. You can have multiple NSURLConnections in flight at once, each with its own delegate.
If you want to know why your case isn't working, you'll probably need to show your code in your question.
I hope that at least answers the general question.

Which NSURLConnection wrapper handles GET and POST equally well?

Which NSURLConnection wrapper handles GET and POST equally well ?
For GET method, I prefer google's GTMHTTPFetcher over ASIHTTPRequest. ASIHTTPRequest uses delegate, which probably the idea you will come up with normally. But that's the exactly reason I chose not to use it because when you have several connections(many connections in my case), then each connection has its own delegate and you end up with too many object. Or you can have just 1 delegate but you have find a way to find out which response is for which connection.
GTMHTTPFetcher handle this way much better in my opinion. It uses 1 SEL for 1 connection, sorta like target-action model. The code is much cleaner than delegate model.
But for POST method, ASIHTTPRequest has ASIFormDataRequest. I did not find an easy way to do POST with GTMHTTPFetcher. It does have setPostData method to set post data. But you have to set post body and those mime parameters by yourselves(from what I have see) And that's the headache. I find it has another class called GTMHTTPUploadFetcher. But I can't really figure out how to use it (I keep getting the NSAssert "need upload location hdr").
So for POST, I guess ASIHTTPRequest is easier.
I did not get a chance to use facebook-ios-sdk. And would like to hear other opinion about it.
So is there NSURLConnection wrapper handle both GET and POST well ? And any idea how to use GTMHTTPUploadFetcher?
The GTMMIMEDocument class is used to create a stream for uploading via GTMHTTPFetcher (or via anything else that takes an NSInputStream.) An example is here.
GTMMIMEDocument keeps a sparse list of the data parts to be uploaded, avoiding duplicating the data in memory.

GET http request method after redirect when using nsurlconnection SynchronousRequest

Setting NSMutableURLRequest with setHTTPMethod:#"HEAD", as i only want the HEAD information, and using NSURLConnection with sendSynchronousRequest, as i need the thread to block until the info is retrieved, all works well until URL redirect occurs. It appears that synchronousRequest uses a GET request after it encounters a redirect and downloads the content in the background (Not what i want). Need someone to shine some light on this. Either how to do synchronous request of just the HEAD after redirect or how to block NSOperation (where an NSURLConnection is used) until NSURLConnection asynchronously fetches the header information.
Thanks
You need to instantiate an NSURLConnection and use it with a delegate. I thought by default redirects did the right thing, but if not, implement -connection:willSendRequest:redirectResponse: to ensure the new request is a HEAD.
You can either:
Implement this on the main thread
Subclass NSOperation to be "concurrent" and run the connection asynchronously there
Run the runloop until the connection finishes, creating the equivalent of a synchronous connection