manuyavuz
manuyavuz

Reputation: 176

How to stop UIWebView loading immediately

As ios documentation says, [webView stopLoading] method should be used in order to stop webview load task.

As far as I see, this methods runs asynchronously, and does NOT stop currently processing load request immediately.

However, I need a method which will force webview to immediately stop ongoing task, because loading part blocks the main thread, which could result in flicks on animations.

So, is there a way to succeed this?

Upvotes: 4

Views: 5503

Answers (2)

ani
ani

Reputation: 245

This worked for me.

if (webText && webText.loading){
    [webText stopLoading];
}

webText.delegate=nil;


NSURL *url = [NSURL URLWithString:@""];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webText loadRequest:requestObj];

Upvotes: 2

FeAt
FeAt

Reputation: 1

Don't use the UIWebView to download your html document directly. Use async download mechanism like ASIHTTPRequest to get your html downloaded by a background thread. When you get the requestFinished with a content of your html then give it to the UIWebView.

Example from the ASIHTTPRequest's page how to create an asynchronous request:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}     

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];
 
   // Use when fetching binary data
   NSData *responseData = [request responseData];
}     

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

Use the responseString to your UIWebView's loadHTMLString method's parameter:

UIWebView *webView = [[UIWebView alloc] init];
[webView loadHTMLString:responseString baseURL:[NSURL URLWithString:@"Your original URL string"]];

Upvotes: 0

Related Questions