25

I am loading some file from document directory using UIWebView. I have set the delegate of UIWebView and I am responding to 2 methods of delegate that is webViewDidStartLoad and webViewDidFinishLoad I am receiving the webViewDidStartLoad But I am not receiving webViewDidFinishLoad method.

Below is the code:

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

Please let me know what is going wrong. I am not getting any error in didFailLoadWithError delegate method.

Note:- the file that I am loading are huge say 3 MB.

Thanks

=============EDITED==================

As I was loading very huge File the delegate was coming after very long duration that I was not able to notice but for small files everything is working fine

Ekra
  • 3,241
  • 10
  • 41
  • 61
  • You can also reduce `NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", pathString]];` to `NSString *path = [documentsDirectory stringByAppendingPathComponent:pathString];` – Deepak Danduprolu May 17 '11 at 13:04
  • http://stackoverflow.com/questions/8986963/how-to-set-my-web-view-loaded-with-already-login-user-iphone – iOS dev Jan 24 '12 at 13:34

6 Answers6

27

Hey probably you should do this,

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

Call removeLoadingView method in the following UIWebViewDelegate methods

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

  [self removeLoadingView];   
   NSLog(@"finish");   
}


-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    [self removeLoadingView];
    NSLog(@"Error for WEBVIEW: %@", [error description]);
}

The showActivityIndicator method

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

The removeLoadingView method

-(void) removeLoadingView
{
   [loadingView removeFromSuperView];
}
SayeedHussain
  • 1,696
  • 16
  • 23
8

Well, does your webview actually finish loading? You should implement the webView:didFailLoadWithError:, too to catch failures.

Since you're not getting either the failure or success message. There might be something wrong with the data that you're trying to load.

Try telling the webView to load an HTML string ("Hello World!"), and see if that succeeds. If it does, then the problem is with your data resource or the path to it.

CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
  • I have implemented the error but it doesn't get in that method. I will just edit my question – Ekra May 17 '11 at 13:10
  • 1
    Well, what actually does happen? Does the webview successfully load the content? – CharlieMezak May 17 '11 at 13:12
  • WebView successfully loads the content . But as I am displaying very huge file say 3MB it takes lot of time. So I wanted to show some progress to user but since I am not able to get the right delegate method called I am not able to do so. – Ekra May 17 '11 at 13:21
  • Something is wrong if neither of the finished/failed delegate methods are being called… Do you still not get either of them if you load a small piece of data in the webview, like a simple html string? – CharlieMezak May 17 '11 at 13:24
  • As I was uploading very huge File. The delegate was taking so much time that I didn't notice. For small files everything is working fine. Thanks for your help. @CharlieMezak:- thanks for pointing out the defect. – Ekra May 17 '11 at 13:31
3

WebView Delegates

  1. Download MBProgessHUD
  2. Add to your Project
  3. When the page begins load loading (*) progess started and hide after page loaded successfully

.h file

    @interface WebViewViewController : UIViewController <MBProgressHUDDelegate,   UIWebViewDelegate>
   {
     MBProgressHUD *HUD;

   }

.m File

- (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];
}
Girish Nair
  • 5,148
  • 5
  • 40
  • 61
Prithivi
  • 101
  • 7
2

Make sure to implement the delegate ( ) in the view's .h file and connect the delegate to the view. This fixed it for me.

woakley5
  • 304
  • 3
  • 17
2

Probably it is experiencing an error. Implement –webView:didFailLoadWithError: and check.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
0

.h file

@property (retain, nonatomic) IBOutlet UIWebView *webview;

.m file

 -(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;
}
Gami Nilesh
  • 581
  • 4
  • 19