开发者

webViewDidFinishLoad not firing when UIWebView opens a PDF

开发者 https://www.devze.com 2023-02-22 00:25 出处:网络
Here is my code: - (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)requestnavigationType:(UIWebViewNavigationType)navigationType;{

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 PD开发者_Go百科F 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;


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号