【问题标题】:webViewDidFinishLoad not working?webViewDidFinishLoad 不工作?
【发布时间】:2014-10-01 18:52:22
【问题描述】:

在我的应用程序中,我试图在加载 UIWebView 时显示初始图像,因此它不仅仅是一个空白屏幕。但是我的 webViewDidFinishLoad 方法不起作用。这意味着一旦 UIWebView 加载,启动图像就会出现,但不会从屏幕上消失。

我的方法代码是:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     NSLog(@"content loading finished");
    [loadingImageView removeFromSuperview];  
}

任何关于为什么该方法不起作用的帮助将不胜感激。

我的.h:

@interface ViewController : UIViewController

-(IBAction)makePhoneCall:(id)sender;

@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;

@end

我的 ViewDidLoad 和 webViewDidFinishLoading:

- (void)viewDidLoad {
    UIWebView *mWebView = [[UIWebView alloc] init];
    mWebView.delegate = self;
    mWebView.scalesPageToFit = YES;

    [super viewDidLoad];
}


//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sleafordpizza.com/food"]]];


//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:@"LittleItalyLogo.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"LittleItalyLogo.png"],
                                    nil];
[self.view addSubview:loadingImageView];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     NSLog(@"content loading finished");
    // Remove loading image from view
    [loadingImageView removeFromSuperview];
}

【问题讨论】:

  • 您是否设置了网络视图的delegate

标签: ios uiwebview


【解决方案1】:

您好,您可能没有设置正确的委托。

这是给你的小代码提示。

-(void)viewDidLoad {

    mWebView = [[UIWebView alloc] init];
    mWebView.delegate = self;
    mWebView.scalesPageToFit = YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {

  [loadingImageView removeFromSuperview];  
   NSLog(@"finish");   
}

在你的.h文件中添加。

@interface MyView: UIViewController <UIWebViewDelegate> {
           UIWebView *webView;
}

代码修复。

对于.h文件

@interface ViewController : UIViewController<UIWebViewDelegate>


-(IBAction)makePhoneCall:(id)sender;

@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;


@end

对于 .m 文件

- (void)viewDidLoad
{


[super viewDidLoad];

webView.delegate = self;

//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sleafordpizza.com/food"]]];


//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:@"LittleItalyLogo.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"LittleItalyLogo.png"],
                                    nil];
[self.view addSubview:loadingImageView];

}

【讨论】:

  • 请显示.h文件和viewDidLoad方法
  • 上线:mWebView.delegate = self;它给了我错误:从不兼容的类型“ViewController *const_strong”分配给“id
  • 请在您的问题中显示 .h 文件。
  • webViewDidFinishLoad 现在可以工作了,但是在加载网页后,它会立即与启动图像一起消失,留下一个空白屏幕。
【解决方案2】:

在某些时候,这个委托方法实际上永远不会被触发。在我的一些项目中,我遇到了同样的问题。

有一次,我实际上不得不使用计时器来解决它,每隔一秒左右检查一次网络视图的状态,看看我是否可以继续。

在这种特殊情况下,我只需要存在某个元素。尽管如此,由于注入了外部脚本错误,视图并未触发完成加载事件。

所以,我只是在 web 视图开始加载时启动了一个触发器,然后不时调用一个方法来查看 web 视图是否包含有问题的元素。

- (void)methodCalledByTimer {
     if (<I still do not have what I need>) {
        //The web view has not yet finished loading; keep checking
     } else {
        //The web view has finished loading; stop the timer, hide spinners and proceed
     }
}

如果绝对有必要,您还可以检查 Web 视图是否实际加载:

- (void)methodCalledByTimer {
     if (self.webView.isLoading) {
        //The web view has not yet finished loading; keep checking
     } else {
        //The web view has finished loading; stop the timer, hide spinners and proceed
     }
}

然后,当然,我也会检查 finishedLoading 事件,以确保确定。记住也要实现 webView:didFailLoadWithError: 方法。

在等待网页完成加载时,需要注意一些事项。

例如,您真的需要它来停止加载,还是您可以做些什么?就我而言,我需要一个元素。能够正确执行脚本是可能需要的另一件事。

其次,加载页面是否使用任何外部资源?我曾经有外部脚本错误导致 webViewDidFinishLoad: 方法根本没有被调用。如果我删除了外部脚本,它就可以工作。

第三,如果页面使用了外部资源,你不仅会看到自己资源的负载能力,还会暴露于外部资源的负载能力。跟踪脚本、广告等...如果某个资源提供者提供内容的速度很慢(或根本不提供),您的页面可能会永远停留在加载状态。

所以,我会去检查其他东西。 :)

【讨论】:

    【解决方案3】:

    我看到你没有处理错误。如果有错误,所有后续的委托调用都不会发生。当 webview 也使用插件时,我惊讶地发现这是真的。它调用此错误方法,告诉您 webview 已移交给委托,在我的情况下是电影播放器​​。

    实现这个,看看是不是这样。

    -(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        if (error.code == 204) {
            //request was handled by a plugin instead of by the webview directly
            ...
        }
        else
        {
            NSLog(@"didFailLoadWithError. ERROR: %@", error);
        }
    }
    

    我能够在这个方法中完成所有剩余的加载工作,而不是 webviewdidfinishLoad

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2011-08-22
      • 2011-12-13
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多