Downloading files WKWebView ios

前端 未结 3 1392
情书的邮戳
情书的邮戳 2021-02-06 16:03

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

3条回答
  •  我在风中等你
    2021-02-06 16:45

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

提交回复
热议问题