How to stop UIWebView loading immediately

风流意气都作罢 提交于 2019-12-30 05:12:09

问题


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?


回答1:


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];



回答2:


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"]];


来源:https://stackoverflow.com/questions/10111918/how-to-stop-uiwebview-loading-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!