我正在加载UIWebview并请求可能需要一些时间才能完成加载的URL.请求开始并开始接收数据.但是,请求似乎没有完成.webViewDidFinishLoad永远不会触发webView didFailLoadWithError:callback.
NSURLRequest的超时是否仅在没有响应接收数据时发生,或者如果请求在该时间间隔内没有完成则触发吗?
如果它是前者,是否有一个优雅的解决方案来超时请求?
相关代码是:
加载请求:
- (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];
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];
[alert hide];
[alert autorelease];
alert = nil;
NSLog(@"error - %@", error);
[self showRetryPrompt: error];
}
- (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];
NSURLRequest的超时用于连接.如果你连接起来并且服务器响应需要很长时间,那么他的超时对你没有好处.您需要为所需的超时设置自己的NSTimer,并自行取消请求.