1

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;
Dan Hanly
  • 7,829
  • 13
  • 73
  • 134
  • Is the pdfViewer delegate set properly? – Joe Apr 05 '11 at 16:48
  • @Joe you were right, however, now the PDF doesn't load at all. Both my webviews are `setDelegate:self`, is this part of the problem? – Dan Hanly Apr 06 '11 at 07:55
  • updated my code with a few minor changes. Still not firing for pdfViewer – Dan Hanly Apr 06 '11 at 08:26
  • That is strange. Try adding this to the loading code `if(webview != pdfViewer && [urlString rangeOfString:@".pdf"].location == NSNotFound)` the `else if(webview != pdfViewer)`. If that works I will post an answer. – Joe Apr 06 '11 at 13:28
  • Thinking two problems were unrelated, I had posted an additional question. The answerer there gave me a clue and I've posted a solution that is pretty similar to your suggestion there. It's win/win in terms of reputation if you drop the answer as above, but below, I'll accept. – Dan Hanly Apr 06 '11 at 13:38

1 Answers1

1

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;
}
Joe
  • 56,979
  • 9
  • 128
  • 135