UIWebView webViewDidFinishLoad没有被调用的iOS

我正在使用UIWebView从文档目录中加载一些文件。 我已经设置了UIWebView的委托,我正在响应委托的两个方法是webViewDidStartLoadwebViewDidFinishLoad我收到webViewDidStartLoad但我没有收到webViewDidFinishLoad方法。

以下是代码:

 @interface MyView: UIViewController <UIWebViewDelegate> { UIWebView *webView; } @property (nonatomic, retain) UIWebView *webView; ========================= Class =========================== -(void)viewDidLoad { CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; mWebView = [[UIWebView alloc] initWithFrame:webFrame]; mWebView.delegate = self; mWebView.scalesPageToFit = YES; [self.view addSubview:mWebView]; NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", pathString]]; [mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]] ]; } // Delegate methods -(void)webViewDidStartLoad:(UIWebView *)webView { NSLog(@"start"); } -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"finish"); } -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"Error for WEBVIEW: %@", [error description]); } 

请让我知道什么是错的。 我didFailLoadWithError委托方法没有得到任何错误。

注意 : – 我正在加载的文件是巨大的说3 MB。

谢谢

============= EDITED ==================

当我加载非常巨大的文件委托来了很长的时间后,我不能注意到,但对于小文件一切工作正常

嘿,大概你应该这样做,

 -(void)viewDidLoad { //webView alloc and add to view CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; mWebView = [[UIWebView alloc] initWithFrame:webFrame]; mWebView.delegate = self; mWebView.scalesPageToFit = YES; [self.view addSubview:mWebView]; //path of local html file present in documentsDirectory NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", pathString]]; //load file into webView [mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]] ]; //show activity indicator [self showActivityIndicator] } 

在下面的UIWebViewDelegate方法中调用removeLoadingView方法

 -(void)webViewDidFinishLoad:(UIWebView *)webView { [self removeLoadingView]; NSLog(@"finish"); } -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [self removeLoadingView]; NSLog(@"Error for WEBVIEW: %@", [error description]); } 

showActivityIndi​​cator方法

 -(void) showActivityIndicator { //Add a UIView in your .h and give it the same property as you have given to your webView. //Also ensure that you synthesize these properties on top of your implementation file loadingView = [UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] loadingView.alpha = 0.5; //Create and add a spinner to loadingView in the center and animate it. Then add this loadingView to self.View using [self.view addSubView:loadingView]; } 

removeLoadingView方法

 -(void) removeLoadingView { [loadingView removeFromSuperView]; } 

那么,你的webview实际上完成加载? 你应该实现webView:didFailLoadWithError:来捕捉失败。

既然你没有得到失败或成功的消息。 您尝试加载的数据可能有问题。

尝试告诉webView加载一个HTMLstring(“Hello World!”),看看是否成功。 如果是这样,那么问题出在你的数据资源或path上。

WebView代表

  1. 下载MBProgessHUD
  2. 添加到您的项目
  3. 当页面开始加载(*)时,页面加载成功后启动并隐藏

.h文件

  @interface WebViewViewController : UIViewController <MBProgressHUDDelegate, UIWebViewDelegate> { MBProgressHUD *HUD; } 

.m文件

 - (void)webViewDidStartLoad:(UIWebView *)webView { HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD]; HUD.delegate = self; HUD.labelText = @"Loading"; [HUD show:YES]; } -(void)webViewDidFinishLoad:(UIWebView *)webView { [HUD hide:YES]; } 

可能正在经历一个错误。 实现–webView:didFailLoadWithError:并检查。

确保在视图的.h文件中实现委托(),并将委托连接到视图。 这为我修好了。

.h文件

 @property (retain, nonatomic) IBOutlet UIWebView *webview; 

.m文件

  -(void)viewDidLoad { NSString *urlAddress = @"YOUR_URL"; _webview.scalesPageToFit = YES; //Create a URL object. NSURL *url = [NSURL URLWithString:urlAddress]; //URL Requst Object NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //Load the request in the UIWebView. [_webview loadRequest:requestObj]; } - (void)webViewDidStartLoad:(UIWebView *)webView; { [self.indicater_web startAnimating]; } //a web view starts loading - (void)webViewDidFinishLoad:(UIWebView *)webView; { [self.indicater_web stopAnimating]; } //web view finishes loading - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; { [self.indicater_web stopAnimating]; } - (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq { [self.indicater_web startAnimating]; return YES; }