Iphone UIWebView webViewDidFinishLoad未调用iOS

Iphone UIWebView webViewDidFinishLoad未调用iOS,iphone,cocoa-touch,ios,uiwebview,Iphone,Cocoa Touch,Ios,Uiwebview,我正在使用UIWebView从文档目录加载一些文件。我已经设置了UIWebView的委托,我正在响应两种委托方法,即 webViewDidFinishLoad和webViewDidFinishLoad我正在接收webViewDidFinishLoad但我没有接收webViewDidFinishLoad方法 代码如下: @interface MyView: UIViewController <UIWebViewDelegate> { UIWebView *webV

我正在使用UIWebView从文档目录加载一些文件。我已经设置了UIWebView的委托,我正在响应两种委托方法,即
webViewDidFinishLoad
webViewDidFinishLoad
我正在接收
webViewDidFinishLoad
但我没有接收
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]);
}
@接口MyView:UIViewController{
UIWebView*webView;
}
@属性(非原子,保留)UIWebView*webView;
=====================================课堂===========================
-(无效)viewDidLoad{
CGRect webFrame=[[UIScreen mainScreen]applicationFrame];
mWebView=[[UIWebView alloc]initWithFrame:webFrame];
mWebView.delegate=self;
mWebView.scalesPageToFit=是;
[self.view addSubview:mWebView];
NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@“%@”,pathString]];
[mWebView加载请求:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
}
//委托方法
-(void)webView加载:(UIWebView*)webView{
NSLog(@“开始”);
}   
-(无效)webViewDidFinishLoad:(UIWebView*)webView{
NSLog(@“完成”);
}
-(void)webView:(UIWebView*)webView失败加载错误:(NSError*)错误{
NSLog(@“网络视图错误:%@,[错误说明]);
}
请告诉我出了什么问题。我在DidFailLoadRor委托方法中没有得到任何错误

注意:-我正在加载的文件很大,比如说3MB

谢谢

====================================================


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

可能是遇到了错误。实现
–webView:didFailLoadWithError:
并检查。

那么,您的webView真的完成了加载吗?您还应该实现
webView:didFailLoadWithError:
,以捕获失败

因为你没有得到失败或成功的信息。您试图加载的数据可能有问题


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

嘿,也许您应该这样做

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

  • 下载MBProgessHUD
  • 添加到您的项目中
  • 当页面开始加载(*)时,程序开始并在页面加载成功后隐藏

  • .h文件

        @interface WebViewViewController : UIViewController <MBProgressHUDDelegate,   UIWebViewDelegate>
       {
         MBProgressHUD *HUD;
    
       }
    
    - (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];
    }
    
    @property (retain, nonatomic) IBOutlet UIWebView *webview;
    
     -(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;
    }
    

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

    .h文件

        @interface WebViewViewController : UIViewController <MBProgressHUDDelegate,   UIWebViewDelegate>
       {
         MBProgressHUD *HUD;
    
       }
    
    - (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];
    }
    
    @property (retain, nonatomic) IBOutlet UIWebView *webview;
    
     -(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;
    }
    
    .m文件

        @interface WebViewViewController : UIViewController <MBProgressHUDDelegate,   UIWebViewDelegate>
       {
         MBProgressHUD *HUD;
    
       }
    
    - (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];
    }
    
    @property (retain, nonatomic) IBOutlet UIWebView *webview;
    
     -(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;
    }
    

    您还可以通过追加PathComponent:[NSString stringWithFormat:@“%@”,pathString]]来减少
    NSString*路径=[DocumentsDirectoryStringByAppendingPathComponent]
    to
    NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:pathString]我已经实现了这个错误,但它没有进入该方法。我将编辑我的问题好吧,到底发生了什么?webview是否成功加载内容?webview是否成功加载内容。但由于我正在显示非常大的文件,比如说3MB,这需要很多时间。所以我想向用户展示一些进展,但由于我无法调用正确的委托方法,因此我无法这样做。如果没有调用完成/失败的委托方法,则会出现问题…如果在webview中加载一小段数据,您是否仍然无法获得其中任何一个,就像一个简单的html字符串?因为我上传了一个非常大的文件。代表花了太多时间,我没有注意到。对于小文件,一切正常。谢谢你的帮助@CharlieMezak:-谢谢你指出缺陷。