ios - WKWebView download zip file from an indirect link -
i trying capture downloading of zip file wkwebview in ios. after logging in, going link, requesting archive , clicking download button, zip file not downloaded. however, wkwebview display file in web browser, or appears (see screenshots below). when trying download file web view, i'm pulling html file.
can provide direction here, regarding right approach catch , download zip file? note, zip file has no direct link. code wkwebview delegate method, , download handler below.
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 \"sign in\" button log 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]; }
i have tried through using uiwebview well, , getting same result. in android, can accomplished download listener. i'd open approach if available, don't think in ios.
thanks help.
you trying download zip after webview finished loading zip file url.. think approach should stop webview loading zip file url , allow api download file desired path..
something below. snippet below assumes zip file url has extension .zip
- (void)webview:(wkwebview *)webview decidepolicyfornavigationaction:(wknavigationaction *)navigationaction decisionhandler:(void (^)(wknavigationactionpolicy))decisionhandler { nsurlrequest *request = navigationaction.request; nsstring *fileextension = [[request url]absolutestring].pathextension; if(fileextension.length && [fileextension caseinsensitivecompare:@"zip"] == nsorderedsame){ //do not load zip url in webview. decisionhandler(wknavigationactionpolicycancel); //instead use api download file [downloadhandler downloadfilefromurl:[request url] completion:^(nsstring *filepath) { nslog(@"%@",filepath); }]; } //allow other request types load in webview decisionhandler(wknavigationactionpolicyallow); }
would love know if worked you.
Comments
Post a Comment