我正在将我的应用程序从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,而不是文件.
示例网址:
https://example.com/index.php?app=core&module=attach§ion=attach&attach_id=1234=example.zip;
我究竟做错了什么.它在UIWebView之前运行良好,我知道事情的完成方式不同.但是如何从dropbox下载一些东西就好了,但论坛上的附件文件却变得蠢蠢欲动.
任何帮助,将不胜感激
嗯,继续我最终的结果,似乎一切正常.
- (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); } }