如何立即停止UIWebView加载
正如ios文档所述,应该使用[webView stopLoading]
方法来停止webview加载任务。
据我所见,这个方法是asynchronous运行的,不会立即停止当前的处理加载请求。
但是,我需要一个方法,将强制webview立即停止正在进行的任务,因为加载部分块主线程,这可能会导致animation轻弹。
那么,有没有办法成功呢?
这对我有效。
if (webText && webText.loading){ [webText stopLoading]; } webText.delegate=nil; NSURL *url = [NSURL URLWithString:@""]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webText loadRequest:requestObj];
不要使用UIWebView直接下载你的html文档。 使用像ASIHTTPRequest这样的asynchronous下载机制来获取由后台线程下载的html。 当你得到你的html的内容requestFinished
然后把它给UIWebView。
来自ASIHTTPRequest页面的示例如何创build一个asynchronous请求:
- (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]; }
对你的UIWebView的loadHTMLString
方法的参数使用responseString
:
UIWebView *webView = [[UIWebView alloc] init]; [webView loadHTMLString:responseString baseURL:[NSURL URLWithString:@"Your original URL string"]];