NSMutableURLRequest timeout doesn't trigger if data starts to load but not webViewDidFinishLoad
Asked Answered
C

2

2

I am loading a UIWebview and requesting a URL that can take some time to finish loading. The request starts and begins to receive data. However, it doesn't appear that the request finishes. The webViewDidFinishLoad never triggers nor does the webView didFailLoadWithError: callback.

Does a timeout of a NSURLRequest only occur if there is no response receiving data or does it trigger if the request doesn't complete in that interval as well?

If it's the former, is there an elegant solution to timing out the request?

Relevant code is:

Load the request:

- (void) loadRequest { NSString *targetURL = @"http://myrequestUrl/"; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: targetURL] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 15.0] autorelease];

[request setHTTPBasicID: [[self credentialManager] userID] password:[[self credentialManager] password]];
[webView loadRequest:request];

Delegate to respond to timeout

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO]; [alert hide]; [alert autorelease]; alert = nil; NSLog(@"error - %@", error); [self showRetryPrompt: error];
}

Citified answered 10/12, 2009 at 20:44 Comment(0)
S
5

The timeout for the NSURLRequest is for the connection. If you get connected and it just takes a long time for the server to response, he timeout will do you no good. You will need to setup your own NSTimer for the timeout you want and cancel the request yourself.

Subjunctive answered 10/2, 2010 at 23:58 Comment(0)
S
0

Agree with user158743.

Using NSTimer, set the timer to start at the webViewDidStartLoad delegate method, and then make sure that you have counters at the webViewDidFinishLoad and webViewDidStarLoad methods to check if the webView has been loaded (this is an ugly hack, but unless you use javascript to check the number of elements in your page, there is really no way to know how many times these 2 methods will be called).

After doing so, do a check of the counters at the method that you are running after the timer has reached its time, and that should work.

The NSTimer code is here --> http://www.iphonedevsdk.com/forum/iphone-sdk-development/14403-nstimer-examples.html

Hope this helps, it did for me. Although I know there are flaws, it works for me as I am loading a splash screen which is supposed to be lightweight in the first place. I'm still looking for solutions though, will post edits here if I have other alternatives.

Feedback is greatly welcomed!

Symploce answered 22/6, 2012 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.