Search code examples
iosipaduiviewuiwebviewuiwebviewdelegate

UIWebView loadRequest reuse gives flickering effect


I have multiple pdf files. Based on user input i am loading the pdf using UIWebView. On first loadRequest it loads the pdf properly. From second call to LoadRequest onwards its showing some flickering effect while loading the new pdf.. Meaning that starts blur display of content and slowly display content properly in few secs.

Code snippet below:

- (void) loadDocument: (NSString *) documentName
{
    NSString * path = [[NSBundle mainBundle] pathForResource: documentName ofType: self.docType];
    NSURL * url = [NSURL fileURLWithPath: path];
    request = [NSURLRequest requestWithURL: url];
    [PdfWebView loadRequest: request];    
}


- (void) loadNewDoc:(int)segIndex
{
    switch (mPageIndex) 
    {
                case 0:
                    [self loadDocument:@"PDF_0"];
                    break;

                case 1:
                    [self loadDocument:@"PDF_1"];
                    break;


                case 2:
                    [self loadDocument:@"PDF_2"];
                    break;


                default:
                    break;
        }
}

Solution

  • You can clear the web view before starting the new request by using

    [yourwebview loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];
    

    Or even

    [yourwebview stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];
    

    This may remove the flickering effect