UIWebView webViewDidFinishLoad没有被调用iOS

问题描述:

我使用UIWebView从文档目录加载一些文件。我已经设置了UIWebView的委托,并且我正在响应代理的两种方法,即 webViewDidStartLoadwebViewDidFinishLoad我正在接收webViewDidStartLoad但我没有收到webViewDidFinishLoad方法。UIWebView webViewDidFinishLoad没有被调用iOS

下面是代码:

@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 ==================

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

+0

您也可以减少'的NSString *路径= [documentsDirectory stringByAppendingPathComponent:[的NSString stringWithFormat:@ “%@”, pathString]];'to'NSString * path = [documentsDirectory stringByAppendingPathComponent:pathString];' – 2011-05-17 13:04:21

+0

http://stackoverflow.com/questions/8986963/how-to-set-my-web-view-loaded-with-already-登录-user-iphone – 2012-01-24 13:34:06

嘿或许你应该这样做,下面的UIWebViewDelegate方法

-(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] 
} 

呼叫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]; 
} 
+0

这是显示webview加载您的请求的正确方法... – 2016-10-21 04:38:14

可能它遇到了一个错误。执行–webView:didFailLoadWithError:并检查。

+0

我编辑了我的问题。请看看它。 – Ekra 2011-05-17 13:19:46

那么,你的web视图实际上完成加载?你也应该执行webView:didFailLoadWithError:以捕捉失败。

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

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

+0

我已经实现了这个错误,但它没有得到这个方法。我将只编辑我的问题 – Ekra 2011-05-17 13:10:37

+1

那么,究竟发生了什么? webview是否成功加载内容? – CharlieMezak 2011-05-17 13:12:31

+0

WebView成功加载内容。但是,由于我显示非常大的文件,说3MB,它需要很多时间。所以我想向用户展示一些进展,但由于我无法获得称为“我无法这样做”的正确委托方法。 – Ekra 2011-05-17 13:21:32

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

确保实现视图的.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; 
}