In a WKWebView
, when a user clicks a link that refers to certain file types (e.g. a VCF file for contacts, or an ICS file for calendar events), I'd like to intercept the link, i.e. cancel the navigation, and instead display the content using a specialized view controller.
For example, the CNContactViewController
can be used to display contacts, the EKEventViewController
can be used to display calendar events.
I can intercept the click by assigning a WKNavigationDelegate
and using decidePolicyForNavigationAction
:
// Swift 2
extension MyController: WKNavigationDelegate {
func webView(webView: WKWebView, decidePolicyForNavigationAction
navigationAction: WKNavigationAction,
decisionHandler: (WKNavigationActionPolicy) -> ()) {
let url = navigationAction.request.URL!
if url.pathExtension == "ics" {
decisionHandler(WKNavigationActionPolicy.Cancel)
// TODO: download data
// TODO: display with EKEventViewController
} else if url.pathExtension == "vcf" {
decisionHandler(WKNavigationActionPolicy.Cancel)
// TODO: download data
// TODO: display with CNContactViewController
} else {
decisionHandler(WKNavigationActionPolicy.Allow)
}
}
}
But in order to display the files the specialized controllers, I need to download the data from the given url
first.
How can I do that?
Since the download requires authentication, the download needs to share the cookies with the WKWebView
, or use another technique to share the already authenticated session.
If it helps: I've already got access to the web view's WKProcessPool
and WKWebViewConfiguration
. To my understanding, the cookies are somehow tied to the WKProcessPool
. But I don't know how to use this to download the content, for example with a NSURLSession
.