UIWebView 打开 PDF 时 webViewDidFinishLoad 未触发

发布于 2024-10-30 20:11:32 字数 2002 浏览 6 评论 0原文

这是我的代码:

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

}

基本上,它的作用是从我的 UIWebView webView 中检测 PDF 链接,并将其加载到另一个 UIWebView pdfViewer (在名为 topView 的视图上找到)。然后我有一个如下的函数:

- (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];
                 }];

}

上面的函数对于 pdfViewer Web 视图根本不会触发,但对于 webView Web 视图会触发。我该如何解决这个问题?

这是我在 viewDidLoad 方法上对两个 webView 的设置。

//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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2024-11-06 20:11:32

确保 pdfViewer 委托设置正确,您可能需要调整加载代码。

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文