下载文件WKWebView iOS [英] Downloading files WKWebView ios

查看:1035
本文介绍了下载文件WKWebView iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序从UIWebView迁移到WKWebView。一切进展顺利,我将对其进行更多修改。但是,我现在注意到我无法下载论坛附件。

I'm in the progress of migrating my app from UIWebView to WKWebView. All is going well and working as I tinker more with it. However, I notice now that I can't download forum attachments.

我正在使用HCDownload,到目前为止,它一直对我来说很完美,所以我知道它不是为此。我相信它的要求,但我无法解决。

I'm using HCDownload, and thus far has always worked perfect for me, so I know it's not on that end. I believe its the request, but I cant figure it out.

我知道以下几点:

UIWebView => WKWebView Equivalent
--------------------------------------------------------------
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction

因此,我正在尝试以下操作:

So with that known I am trying below:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    NSURLRequest *request = navigationAction.request;
    NSURL *fileURl = [request URL];
    NSString *externalFileExtension = [fileURl pathExtension];
    NSString *internalFileExtension = [[fileURl absoluteString] pathExtension];

    HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
    UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:dlvc];
    dlvc.delegate = self;
    vc.transitioningDelegate  = self;

    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {

        //External file extensions
        if ([fileExtensions containsObject:[externalFileExtension lowercaseString]]) {
            [dlvc downloadURL:fileURl userInfo:nil];
            [self presentViewController:vc animated:YES completion:nil];
            [vc release];

            NSLog(@"externalURL is %@", fileURl);
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }

        //Internal file links
        if ([fileExtensions containsObject:[internalFileExtension lowercaseString]]) {
            [self presentViewController:vc animated:YES completion:nil];
            [dlvc downloadURL:fileURl userInfo:nil];
            [vc release];

            NSLog(@"internalURL is %@", fileURl);
            decisionHandler(WKNavigationActionPolicyCancel);
            return;

        }
}

它将触发我的下载控制器找到文件扩展名,而是下载index.php,而不是文件。

It will fire my download controller by finding the file extension, but instead it downloads the index.php, not the file.

示例网址:

https://example.com/index.php?app=core&module=attach&section=attach&attach_id=1234=example.zip;

我在做什么错。以前在UIWebView中运行良好,而且我知道事情做得不同。但是如何从Dropbox上下载东西就很好了,但是论坛上的附件却被搞砸了。

What am I doing wrong. It worked fine before in UIWebView and I know things are done differently. But how can it download something from say dropbox just fine, but an attached file on a forum gets goofed up.

任何帮助将不胜感激

推荐答案

这就是我最终得到的结果,并且一切似乎都正常。

Well heres what I ended up with, and all seems to be working fine.

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
    NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }

    decisionHandler(WKNavigationResponsePolicyAllow);
    //NSLog(@"decidePolicyForNavigationResponse");
}


//Download manager
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    NSURLRequest *request = navigationAction.request;
    fileURL = request.URL;

    HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
    UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:dlvc];
    vc.transitioningDelegate  = self;
    dlvc.delegate = self;

    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {

        //Internal file links
        NSString *internalFileExtension = fileURL.absoluteString.pathExtension;
        if ([fileExtensions containsObject:[internalFileExtension lowercaseString]]) {

            //Fire download
            [dlvc downloadURL:fileURL userInfo:nil];
            [self presentViewController:vc animated:YES completion:nil];
            [vc release];

            NSLog(@"internalURL is %@", fileURL);
            if (decisionHandler) {
                decisionHandler(WKNavigationActionPolicyCancel);
            }
            return;
        }

        //External file extensions
        NSString *externalFileExtension = fileURL.pathExtension;
        if ([fileExtensions containsObject:[externalFileExtension lowercaseString]]) {

            //Fire download
            [dlvc downloadURL:fileURL userInfo:nil];
            [self presentViewController:vc animated:YES completion:nil];
            [vc release];

            NSLog(@"externalURL is %@", fileURL);
            if (decisionHandler) {
                decisionHandler(WKNavigationActionPolicyCancel);
            }
            return;
        }
    }

    if (decisionHandler) {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

这篇关于下载文件WKWebView iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆