简体   繁体   中英

WKWebView Check if web view support to load a file

I am create a iOS Application. I am using WKWebView at some stage to show some files of any extension types. For Audio, Video, html, text, pdf, documents files its working well. But files like.zip, .csv and many others its not showing proper data or show blank screen without any error I am getting in its navigations delegates methods.

My Question is how can I check if my WKWebView is able to load/support that file? So that if it's not able to load or support that file, I can show another view where I will direct download that file to File manager and give user option to save or share that file.

Like In Safari browser when I am trying to load zip file its not show any preview, its show an alert to download (in iOS 13) or show Save and share file action (in iOS 13). How Safari is detecting that its support that file or not?

Please do not suggest to use direct String matching for mime types or extensions because there can be 1000+ files extensions in the world.

You need to make your controller as webView.navigationDelegate and define the navigation response delegate callback as follows:

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, 
    decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

    if navigationResponse.canShowMIMEType {
        decisionHandler(.allow)
    } else {
        decisionHandler(.cancel)
        // do something else with `navigationResponse.response`
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM