我试图从iOS中的WKWebView捕获zip文件的下载 . 登录后,转到此link,请求存档并单击下载按钮,不会下载zip文件 . 但是,WKWebView确实在Web浏览器中显示该文件,或者似乎这样做(请参见下面的屏幕截图) . 当尝试从Web视图下载文件时,我只是提取HTML文件 .
关于捕获和下载zip文件的正确方法,有人可以提供一些方向吗?注意,zip文件没有直接链接 . WKWebView委托方法的代码和下面的下载处理程序 .
webView didFinishNavigation
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSURLComponents *comps = [[NSURLComponents alloc] initWithURL:webView.URL
resolvingAgainstBaseURL:NO];
comps.query = nil;
NSLog(@"did finish nav URL: %@", webView.URL);
if ([webView.URL.absoluteString isEqualToString:LI_DOWNLOAD_URL]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[DownloadHandler downloadFileFromURL:webView.URL completion:^(NSString *filepath) {
NSLog(@"%@",filepath);
}];
});
}
else if ([comps.string isEqual: LI_REDIRECT_CATCH1] ||
[comps.string isEqual: LI_REDIRECT_CATCH2] ||
[comps.string isEqual: LI_REDIRECT_CATCH3]) {
self.statusText.text = @"Tap the \"Sign In\" button to log into LinkedIn";
}
else if ([comps.string isEqual: LI_EXPORT_PAGE]) {
NSString *javascript = @"javascript:" \
"var reqBtn = document.getElementById('request-button');" \
"var pndBtn = document.getElementById('pending-button');" \
"var dwnBtn = document.getElementById('download-button');" \
"if (reqBtn) {" \
" window.scrollTo(reqBtn.offsetLeft, 0);" \
" window.webkit.messageHandlers.dataExport.postMessage('willRequestData');" \
" reqBtn.addEventListener('click', function() {" \
" window.webkit.messageHandlers.dataExport.postMessage('didRequestData');" \
" }, false);" \
"} else if (pndBtn) {" \
" window.scrollTo(pndBtn.offsetLeft, 0);" \
" window.webkit.messageHandlers.dataExport.postMessage('isRequestPending');" \
"} else if (dwnBtn) {" \
" window.scrollTo(dwnBtn.offsetLeft, 0);" \
" window.webkit.messageHandlers.dataExport.postMessage('willDownloadData');" \
" dwnBtn.onclick = function() {" \
" window.webkit.messageHandlers.dataExport.postMessage('didDownloadData');" \
" };" \
"}";
[self.webView evaluateJavaScript:javascript completionHandler:nil];
}
}
Download Handler
+ (void)downloadFileFromURL:(NSURL *)url completion:(void (^)(NSString *filepath))completion {
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"linkedin-file-export.zip"];
[data writeToFile:dataPath atomically:YES];
completion(dataPath);
});
}
else {
NSLog(@"%@",error);
completion([NSString string]);
}
}];
[postDataTask resume];
}
我也尝试过使用UIWebView,并得到相同的结果 . 在Android中,这可以通过下载监听器来完成 . 如果它可用,我会接受这样的方法,但我不认为它在iOS中 .
谢谢你的帮助 .
1 回答
您正在尝试在webview完成加载zip文件URL后下载zip ..我认为该方法应该是阻止webview加载zip文件URL并允许您的API将文件下载到所需的路径..
像下面这样的东西 . 下面的代码段假设zip文件网址的扩展名为
.zip
很想知道这是否适合你 .