我正在将我的应用程序从UIWebView迁移到WKWebView。当我对它进行更多的修补时,一切都进行得很顺利,并且工作正常。然而,我注意到现在我不能下载论坛附件。
我使用的是HCDownload,到目前为止,它对我来说一直都是完美的,所以我知道它不是在这一端。我相信这是我的要求,但我想不通。
我知道以下几点:
UIWebView => WKWebView Equivalent
--------------------------------------------------------------
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction因此,鉴于此,我尝试如下:
- (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,而不是文件。
示例url:
https://example.com/index.php?app=core&module=attach§ion=attach&attach_id=1234=example.zip;我做错了什么。它以前在UIWebView中工作得很好,我知道事情是不同的。但是,它怎么能从dropbox上下载一些东西,但是论坛上的一个附件却搞砸了。
任何帮助都将不胜感激
发布于 2016-01-12 17:58:21
这就是我最终得到的结果,一切似乎都运行得很好。
- (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);
    }
}发布于 2016-01-05 15:46:50
您可能正在下载一个未经授权的页面,因为您的WKWebView和HCDownload实例不像UIWebView那样共享cookie会话。对于WK2流程模型的速度和安全性改进来说,这是一个必要的权衡。
我通过实现_WKDownloadDelegate来added downloading到我的基于WKWebKit的OSX/Swift迷你浏览器。从El Cap和iOS9开始,这是一个完全没有文档记录的私有协议。它允许你的导航代理调用decisionHandler(_WKNavigationResponsePolicyBecomeDownload),在给你选择/更改文件名的机会后,WebKit将在后台下载。我还不知道如何在iOS中实现文件选择器,也不知道App Store的审阅者是否会允许使用该协议。但是我的应用程序现在可以正确地处理从像PHP论坛这样的会话身份验证站点的下载。
发布于 2018-03-21 22:41:35
Swift 4:
ChrisOSX的解决方案也解决了我的问题。登录网站后,我正在尝试从该网站上的链接下载文档。我已经使用WKZombie抓取了链接,然后我想使用Alamofire下载文件。我添加了这个
if let httpResponse = navigationResponse.response as? HTTPURLResponse, let url = httpResponse.url {
    let allHeaders = httpResponse.allHeaderFields.reduce([String : String](), { (result, next) -> [String : String] in
    guard let stringKey = next.key as? String, let stringValue = next.value as? String else { return result }
        var buffer = result
        buffer[stringKey] = stringValue
        return buffer
    })
    let cookies = HTTPCookie.cookies(withResponseHeaderFields: allHeaders, for: url)
    for cookie in cookies {
        HTTPCookieStorage.shared.setCookie(cookie)
    }
}至:
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)在此之后,cookies被存储在共享cookies中,我可以使用Alamofire下载文件,而不需要更改下载方法。
https://stackoverflow.com/questions/34547161
复制相似问题