0

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.

Community
  • 1
  • 1
bph
  • 189
  • 2
  • 14

1 Answers1

2

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.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • Thanks for the answer! I basically tried what you said, and I used NSURLProtocol:registerclass: inside my view controller. However, I was wondering, is there any way for my custom class derived from NSURLProtocol to set a view controller as a delegate inside my view controller? Thanks! – bph Jun 17 '12 at 06:10
  • Interesting question. I don't know off the top of my head, I'd have to go and do some investigations. I'm sure there will be some sort of solution, its just finding which is the most easy/elegant. If nothing else it could post a notification. – Gruntcakes Jun 17 '12 at 21:21
  • Do you have any clues as to any options that are elegant? If I were to create a delegate in the custom NSURLProtocol class, I would need an instance of the class to set it right? – bph Jun 18 '12 at 01:35
  • I haven't had the chance to think about this, but you're right that that not having an instance is the problem. You could post custom notifications from the protocol to the controller. – Gruntcakes Jun 18 '12 at 14:10
  • Perfect, the custom notifications work fine. Thanks for all the help! – bph Jun 18 '12 at 20:33