Search code examples
iphoneiosobjective-cipadios6

webViewDidStartLoad and ..FinishLoad are called sequentially


I am developing an app, which uses UIWebView. I am implementing the manual load counting method as in UIWebView - How to identify the "last" webViewDidFinishLoad message?

I can see the calls for start/finish/failLoad balanced in principle. However, with a few web pages (most particularly, google.com), the page initially loads well and after a few seconds automatically (seems to) refresh itself. The start/finish sequence looks as below:

-[MyWebView webViewDidStartLoad:], 1, 0x895150c
-[MyWebView webView:didFailLoadWithError:], 0, 0x895150c
-[MyWebView webViewDidStartLoad:], 1, 0x895150c
-[MyWebView webViewDidStartLoad:], 2, 0x895150c
-[MyWebView webViewDidFinishLoad:], 1, 0x895150c
-[MyWebView webViewDidFinishLoad:], 0, 0x895150c
-[MyWebView webViewDidStartLoad:], 1, 0x895150c   <-- after a few seconds' gap
-[MyWebView webViewDidFinishLoad:], 0, 0x895150c

The first argument is the function being called, second is the value of the counter and the last value is the address of the loadCounter, just to confirm that it is the same webView instance (and hence the counter) being called. I am running out of clues :(...

Does anyone have any clue, what might be happening here? (BTW, this does not happen for many other websites, so far only with Google)

EDIT: Why it matters to me is, because while it refreshes (apparently randomly), it dismisses the keyboard. This is annoying if a user is already in the middle of typing something. Is it possible to relegate any further refreshes to the site in the background, while continuing the user interaction (particularly keyboard) in the front?

TIA, Nikhil


Solution

  • As a workaround to avoid the issue - You can listen to keyboard show/hide notifications. Then when you know the keyboard is used, do not allow the web view to perform requests

    Update:

    First, you'll need to set your view controller to be the webview delegate so that it could respond to the web view delegate messages:

    @interface myViewController : UIViewController <UIWebViewDelegate>
    

    Then once the webview object is created, set the view controller to be its delegate:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        someWebView.delegate = self;
    }
    

    Now, when the webview will perform its requests it will first consult with the myViewController as it is the associated delegate. This is when you should return NO if you don't want to allow the webview to perform the request.

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
        if (keyboardIsShown == YES){
            return NO;
        }
        else{
            return YES;
        }
    }
    

    How do you know if the keyboard is visible at the moment? see this post