3

Due to an issue where I can't get the NSURLRequest timeout to fire in my UIWebView, I am trying to use an NSTimer to check to see if I've got a response within a designated period. If I haven't, then I want to cancel the request and warn the user.

However: I'm unable to find a way to cancel a current request that has been made in a UIWebView.

How do you do it?

I have setup the request for the UIWebView as follows:

NSString *theURL = [NSString stringWithFormat:@"https://%@%@", _hostname, LOGIN_PART_URL];
NSString *body = [NSString stringWithFormat:@"Username=%@&Password=%@", _username, _password];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:theURL] 
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:_timeoutInSecs];



[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];

[web loadRequest:request];

I can see that NSURLConnection has a cancel method, but there seems to be no equivalent for UIWebView.

What am I missing?

Thanks! Stretch :)

Community
  • 1
  • 1
Stretch
  • 3,669
  • 2
  • 28
  • 40

1 Answers1

10

There is a stopLoading method in UIWebView class. Does it meet your needs?

Per UIWebView Class Reference:

stopLoading

Stops the loading of any web content managed by the receiver.

- (void)stopLoading

Stops any content in the process of being loaded by the main frame or any of its children frames. Does nothing if no content is being loaded.

Hailei
  • 42,163
  • 6
  • 44
  • 69
  • Thanks! I feel dumb for not having found that.. I was tied up with UIWebViewDelegate methods. Cheers :) – Stretch May 17 '12 at 04:21