Read JSON content from UIWebView

iphone, json, uiwebview

Solution

You may want to look into defining a `UIWebViewDelegate` and using `webView:shouldStartLoadWithRequest:navigationType:` to intercept the request and handle it.

In your case, you would evaluate the `NSURLRequest` and return `NO` to prevent the WebView from loading. In turn, you would create a separate `NSURLConnection` with the same request, and set up a delegate to receive the (JSON) response.

Problem

I'm using a UIWebView to display a captcha. If the user enters the captcha correctly, then the server returns data using JSON serialization. I don't want the view to display this, instead I want to intercept the loads of the UIWebView, and if it returns JSON serialized data, I want to store that data and remove the UIWebView. I was thinking of setting up a delegate to the UIWebView and use its webViewDidFinishLoad, but how do I get the content loaded?

Original source