我正在将我的应用从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下载一些东西就好了,但是论坛上的附件文件会变得很糟糕。
任何帮助将不胜感激
答案 0 :(得分:5)
嗯,继续我最终的结果,似乎一切正常。
- (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);
}
}
答案 1 :(得分:3)
您可能正在下载未经授权的页面,因为您的WKWebView和HCDownload实例不像UIWebView那样共享cookie会话。这是WK2过程模型速度和速度的必要权衡。安全改进。
通过实现_WKDownloadDelegate,我added downloading到我的基于WKWebKit的OSX / Swift迷你浏览器。它是一个完全没有文档的私人协议,如El Cap和iOS9。它允许您的导航委托调用decisionHandler(_WKNavigationResponsePolicyBecomeDownload)
,以便在您有机会选择/更改文件名后,WebKit将在后台下载。我还没有关于如何在iOS中实现文件选择器的最模糊的想法,也不是App Store审阅者允许使用该协议。但我的应用程序现在可以正确处理从PHP论坛等会话身份验证网站下载。
答案 2 :(得分:1)
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)
在此之后,cookie存储在共享cookie中,我可以在没有Alamofire的情况下下载该文件并更改下载方法。