How to Detect and Handle HTTP Status Codes in UIWebViews
Imagine you have the following problem: You want to load a url in a UIWebView. But the resource at that url requires authentication, and if the user is not authenticated the web page responds with a “403 Not Authenticated” without showing any other instructions on how to login or where to go next etc. Ideally you’d want to redirect the user to a page where she can log in and continue to browse the url. The problem is UIWebView does not provide any properties or methods for inspecting HTTP status codes. One workaround is to intercept the UIWebView request loading process and check whether the user is authenticated or not by loading the same url using an NSURLConnection and inspecting the response, then you can behave accordingly.
In order to check the url, we implement this UIWebViewDelegate method:
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
If session is not checked before, we prevent the request from loading by returning NO and start loading the same request using an NSURLConnection, otherwise we just return YES and the web view continues to load the request.
We inspect the response in NSURLConnection delegate method:
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
The response is actually an NSHTTPURLResponse instance and it provides a method for getting the HTTP status code “- (int) statusCode”. After learning whether or not the user is authenticated we cancel the connection since we don’t need the actual response data. If the user is authenticated we get a “200 OK” and start loading the initial url, otherwise we get a “403 Not Authenticated” and redirect the user to the login page instead of leaving her alone with the empty useless 403 page.
Demo project shows an example situation by trying to load a Google+ photo album url. Here I used this workaround for solving an authentication related problem but the idea is applicable to any problem where you need to know with what HTTP status code the server responds. It might be redirecting the web view to another url if your main url responds with a “503 Service Unavailable” or providing alternate content when the server responds with a “404 Not Found” and so on. You got the idea.
In the end, usability is important. Good usability and providing a nice user experience is not always about the visual design of the product/software. It’s also about the functional behaviour and consistency of the product. Think of an app that looks stunningly beautiful in design but does not work, will it make any sense? No, right? So please try not to leave your users alone with an empty 403 or 404 page, having no idea what to do / where to go next – unless your app itself is not a web browser.
Here is the github readonly url, git clone it to your local repo:
or download the Xcode project from github:
Some screenshots from the demo app running on the iPhone simulator: