How to stop UIWebView loading immediately

故事扮演 提交于 2019-11-30 15:37:09

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

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