UIWebView的webViewDidFinishLoad函数在iOS系统中未被调用

25

我正在使用 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]; - Deepak Danduprolu
https://dev59.com/Yl_Va4cB1Zd3GeqPVKBW - iOS dev
6个回答

27

嘿,也许你应该这样做:

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

showActivityIndicator方法

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

8

你的webview是否已经加载完成?你应该实现webView:didFailLoadWithError:方法,以便捕获失败信息。

由于你既没有收到成功也没有收到失败的消息,可能是你尝试加载的数据出了问题。

试着让webView加载一个HTML字符串(“Hello World!”),看看是否成功。如果成功了,那么问题可能出在你的数据资源或路径上。


我已经实现了错误处理,但它没有进入那个方法。我将编辑我的问题。 - Ekra
1
那么,实际上发生了什么?Webview是否成功加载内容? - CharlieMezak
WebView 成功加载了内容。但是由于我正在显示非常大的文件,比如说 3MB,所以需要很长时间。因此,我想向用户展示一些进度,但由于我无法调用正确的委托方法,所以我无法这样做。 - Ekra
如果已完成/失败的委托方法都没有被调用,那么肯定出了些问题……如果您在webview中加载了一小段数据,比如一个简单的HTML字符串,您仍然无法获取到它们吗? - CharlieMezak
当我上传非常大的文件时,代理花费了很长时间,以至于我没有注意到。对于小文件,一切正常工作。感谢您的帮助。@CharlieMezak:感谢您指出这个缺陷。 - Ekra

3

WebView代理

  1. 下载 MBProgressHUD
  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];
}

2

请确保在视图的.h文件中实现委托 ( ),并将委托连接到视图。这样对我来说就解决了问题。


2

可能它正在遇到错误。实现–webView:didFailLoadWithError:并进行检查。


0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接