0

I use webview in my UIVIewController and load the local HTML file in it using following method.

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];

If I put this method in ViewDidLoad, I can see white flicking while navigating to my controller that doesn't look good.

I tried putting this method in ViewWillAppear like below. I am using webViewLoaded flag to make sure that webview has been loaded then only show the current view else it waits but it is going in infinite loop of waiting!

- (void)viewWillAppear:(BOOL)animated {
    webViewLoaded = NO;

    webView.scalesPageToFit = allowZoom;
    webView.dataDetectorTypes = UIDataDetectorTypeNone;
    webView.delegate = self;

   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];

   int count = 0;

   while(webViewLoaded == NO) 
   {
        NSLog(@"Waiting...");
        [NSThread sleepForTimeInterval:0.1];
        if(count++ > 10) break;
   }
}

- (void)webViewDidFinishLoad:(UIWebView *)localwebView {
    webViewLoaded = YES;
}

I have also tried the same thing in ViewDidLoad but still its going in infinite loop.

Please note that webViewLoaded is "volatile BOOL" but the webview delegate method is not getting called. Not sure what's going on!

Could anyone please help me to fix this. Thanks.

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

3 Answers3

3

First : You're blocking your main thread and not giving WebView any chance to finish loading. Never block your main thread.

Second : There are two UIWebView delegate methods : webViewDidFinishLoad and webView:didFailLoadWithError: - make sure to listen to both.

If you're trying to wait until WebView completes loading and only show your controller afterwards - use delegation or blocks. Please note that this is not a proper way of doing things - i'm just modifying your example to make it work :

child controller (with WebView) :

-(void)startLoadingWithParent:(id)_parent {
  self.parent = _parent;
  NSURL * url = [[NSBundle mainBundle] URLForResource:@"resource" withExtension:@"html"];
  [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
  [parent showMe:self];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  [parent showError:error];
}

master controller :

-(void)doSomething {
   SecondController * ctrl; /// secondController should be created AND has its view loaded at this point
   /// ctrl = [SecondController new];
   /// ctrl.view;
   [ctrl startLoadingWithParent:self];
   /// show user that we're doing something - display activity indicator or something
 }

 -(void)showMe:(UIViewController*)me {
    [self.navigationController pushViewControllerAnimated:me];
 }
epolyakov
  • 75
  • 1
  • 4
  • I did this but didn't work! None of the webview delegate gets called. – Paresh Masani May 03 '12 at 09:44
  • It is actually starting loading when I push my controller and all view cycle methods (viewdidload, viewdidappear etc) exited! – Paresh Masani May 03 '12 at 10:01
  • Please see the commented lines in doSomething. You have to make sure your view is loaded at this point – epolyakov May 03 '12 at 14:10
  • Yah what you mean by that? I am calling "loadview" method before calling loadWebview. Can you tell me how would I load the view but not show it if there is any other way? Thanks! – Paresh Masani May 03 '12 at 14:24
  • From the doc - "loadView : You should never call this method directly. ". Just access the view somehow (ctrl.view for example) to make it load – epolyakov May 03 '12 at 14:31
0

use Following code for

[self performSelectorInBackground:@selector(webViewLoaded) withObject:nil];

so that it will not affect your UI

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
Saad
  • 8,857
  • 2
  • 41
  • 51
  • webViewLoaded is just a flag..not a method! I guess if I load webview in background still I would see the white flicking untill webview has been loaded! – Paresh Masani May 02 '12 at 15:29
0

Try using [NSURL fileURLWithPath:filePath]

shabbirv
  • 8,958
  • 4
  • 33
  • 34
  • Throws the exception - -[NSURL _CFURLRequest]: unrecognized selector sent to instance *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL _CFURLRequest]: unrecognized selector sent to instance 0x4ce2980' – Paresh Masani May 02 '12 at 16:14
  • can you print out what is contained in the "filePath" variable – shabbirv May 02 '12 at 16:49
  • /var/mobile/Applications/.../Documents/web1.html – Paresh Masani May 03 '12 at 09:43