I have a special problem with my iOS App.
My setup is the following:
- Swift 4 app with minimum target iOS 11
- the app consists more or less of single view with a WKWebView which is connected to the client portal of a health insurance
- the client portal provides a login (session cookies) and allows to download pdf documents when logged-in
I want the downloades pdf files to be opened in QuickLookPreviewController instead of WKWebView.
The customer need is that they want to be able to print or share their documents and I like to use the QuickLookPreview therefore, because it offers the best native feeling for this case.
Now to my problem:
I managed to download the documents to local storage (temp folder) and to open it in QuickLookPreviewController with a little trick:
The response of the download is intercepted (by using webView(..., decidePolicyFor ...)) and the download url is used to trigger a separate download (using URLSession.shared.dataTask(with: downloadUrl)) to iPhone storage, because QuickLookPreviewController needs a local file and cannot deal with the download url to the pdf.
In general this works - most of the time. Since I need to be in a logged-in state to download the pdf, I have to pass the authentication cookies from WKWebView (where I logged-in) to the shared cookie storage (which is used by the download task). This cookie sync between the two cookie storages is a known issue and I think I read all the related postings in the web, but couldn't get it working reliably.
In my debug logs I can see that my logic works in general, but sometimes the cookies aren't synchronized at all.
Here's my code. I appreciate every help
P.S. I tried many solutions:
- Reading and syncing the cookies in main thread (there are many hints in the web, that you have to acces cookies in this way) - does not work reliably
- I configured a WKProcessPool - didn't help
Most of the time the cookies are synchronized, but with the same implementation I get problems when trying it later on, e.g. when I uploaded the presumably working code to TestFlight. It even differs when using a solution in Simulator or on a real device.
The error is a downloaded file (HTTP status 200), but its size is less than 8 kB, because I wasn't allowed to download it it because of the missing synchronized authentication cookie.