【问题标题】:UIWebView webViewDidFinishLoad not getting called iOSUIWebView webViewDidFinishLoad 没有被调用 iOS
【发布时间】:2011-08-27 05:24:41
【问题描述】:

我正在使用 UIWebView 从文档目录加载一些文件。我已经设置了 UIWebView 的委托,我正在响应 2 种委托方法,即 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。

谢谢

=============已编辑===================

当我加载非常大的文件时,代理在很长一段时间后才出现,我没有注意到,但对于小文件,一切正常

【问题讨论】:

  • 你也可以将NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", pathString]];缩减为NSString *path = [documentsDirectory stringByAppendingPathComponent:pathString];

标签: iphone cocoa-touch ios uiwebview


【解决方案1】:

嘿,也许你应该这样做,

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

【讨论】:

    【解决方案2】:

    好吧,你的 webview 真的完成加载了吗?你也应该实现webView:didFailLoadWithError: 来捕获失败。

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

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

    【讨论】:

    • 我已经实现了错误,但它没有进入该方法。我将编辑我的问题
    • 嗯,到底发生了什么? webview 是否成功加载内容?
    • WebView 成功加载内容。但是当我显示非常大的文件时说 3MB 这需要很多时间。所以我想向用户展示一些进展,但由于我无法调用正确的委托方法,所以我无法这样做。
    • 如果两个已完成/失败的委托方法都没有被调用,则出现问题……如果您在 webview 中加载一小段数据,例如简单的 html 字符串,您是否仍然无法获得其中任何一个?
    • 当我上传非常大的文件时。代表花了这么多时间,我没有注意到。对于小文件,一切正常。谢谢你的帮助。 @CharlieMezak:- 感谢您指出缺陷。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        确保在视图的 .h 文件中实现委托 ( ) 并将委托连接到视图。这为我解决了问题。

        【讨论】:

          【解决方案6】:

          .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;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-05-04
            • 2011-10-03
            相关资源
            最近更新 更多