Search code examples
objective-cuiwebviewuiwebviewdelegate

webViewDidFinishLoad not firing when UIWebView opens a PDF


Here is my code:

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request  navigationType:(UIWebViewNavigationType)navigationType;  {
/* Add Content Loading Banner */
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    [self.view addSubview:linkLoadView];
    linkLoadView.alpha = 1.0;
}

/* Handle PDF Opening */
NSURL *url = [request URL];
NSString *urlString = [url absoluteString];
if([urlString rangeOfString:@".pdf"].location == NSNotFound){
    return true;
} else {
    NSURL *filePath = [NSURL URLWithString:urlString];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
    [pdfViewer loadRequest:requestObj];
    [self.view addSubview:topView];
    [self.view addSubview:linkLoadView];    
    return false;
}   

}

Basically what this does is detect a PDF link from within my UIWebView webView and loads it into an additional UIWebView pdfViewer (found on a View called topView). I then have a function as below:

- (void) webViewDidFinishLoad:(UIWebView *)theWebView{
        //for webView
        [UIView animateWithDuration:2
                 animations:^{
                      loadingView.alpha = 0.0;
                      linkLoadView.alpha = 0.0;
                 }
                 completion:^(BOOL finished){
                     [loadingView removeFromSuperview];
                     [linkLoadView removeFromSuperview];
                 }];

}

The above function doesn't fire at all for the pdfViewer web view, but does for the webView web view. How do I fix this?

Here is my setup settings for both webViews, on the viewDidLoad method.

//Options for 
webView.delegate = self;
webView.scalesPageToFit = YES;
for (id subview in webView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;

//Options for 
pdfViewer.delegate = self;
pdfViewer.scalesPageToFit = YES;
for (id subview in pdfViewer.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;

Solution

  • Make sure that the pdfViewer delegate is set properly and you will probably need to adjust the loading code.

    - (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request  navigationType:(UIWebViewNavigationType)navigationType;  {
        /* Add Content Loading Banner */
        if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            [self.view addSubview:linkLoadView];
            linkLoadView.alpha = 1.0;
        }
    
        /* Handle PDF Opening */
        NSURL *url = [request URL];
        NSString *urlString = [url absoluteString];
        if(webview != pdfViewer)
        {
            if([urlString rangeOfString:@".pdf"].location == NSNotFound){
                return true;
            } 
            else {
                NSURL *filePath = [NSURL URLWithString:urlString];
                NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
                [pdfViewer loadRequest:requestObj];
                [self.view addSubview:topView];
                [self.view addSubview:linkLoadView];    
                return false;
            }
        }
        return true;
    }