[ Ajax timeout in UIWebView ]
I'm working on an iOS app that displays a webpage through a UIWebView
. The page contains between 20 and 40 buttons. Each one sends an ajax request to the server. It works for the most part, but the server is a desktop app and could close at any moment for any reason with no warning. With these ajax requests, there is no output displayed to the user so if the server went down, the user would have no idea. I would like to be able to be notified of an ajax timeout and then give the user the options to either try again or go back to the home screen. I can't seem to find a way to have the UIWebView
be notified of any failed ajax requests.
At first I thought I could simply use the webView:didFailLoadWithError:
method of the UIWebViewDelegate
, but apparently it's not notified of failed ajax requests.
Any suggestions are greatly appreciated
Answer 1
If when a failure occurs, there is JavaScript code that is invoked in your Ajax failure handlers that makes a location.href change then the UIWebViewDelegate shouldStartLoadWithRequest: method will get called, giving you the chance to do something whenever there is a failure. i.e.
In the Ajax timeout handling code:
window.location.href = "myscheme://AjaxFailure";
In your UIWebViewDelegate handling code:
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"shouldStartLoadWithRequest: url: %@", [[request URL] absoluteString]);
NSURL *URL = request.URL;
if ([URL.scheme isEqualToString:@"myscheme"])
{
... notify the user there was an error