WKWebView xmlhttprequest with file url

后端 未结 3 856
一向
一向 2021-02-14 17:12

I am migrating from UIWebView to WKWebView with my local HTMLs in the app documents folder. I can load the index page with all css and js files, but every ajax call (xmlhttprequ

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 17:14

    So the accepted was not working for me when I first tried it (I was doing it wrong at the time) and the bug about (https://bugs.webkit.org/show_bug.cgi?id=154916) it strongly recommends against doing it. My solution was to implement a custom url scheme and use that to load all files. Looks like this.

    First creating the WKWebView to which the url scheme is attached (you must create the WKWebView yourself, it can't be created on a storyboard).

    override func viewDidLoad() {
    
        let configuration = WKWebViewConfiguration()
        configuration.setURLSchemeHandler(PrayerAssetHandler(), forURLScheme: "x-file")
    
        webview = WKWebView(frame: webContentView.bounds, configuration: configuration)
        self.webContentView.addSubview(webview!)
        webview?.autoresizingMask = webContentView.autoresizingMask
        webview!.navigationDelegate = self
    
        super.viewDidLoad()
    }
    

    Then implement the handler.

    import Foundation
    import WebKit
    import MobileCoreServices
    
    class PrayerAssetHandler: NSObject, WKURLSchemeHandler {
    
        func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
            let url = urlSchemeTask.request.url!
    
            let pathArr = url.path.components(separatedBy: ".")
            let forResource: String = pathArr[0]
            let ofType: String? = pathArr.count > 1 ? pathArr[1] : nil
    
            let bundlePath = Bundle.main.path(forResource: "data_sub_folder" + forResource, ofType: ofType)
    
            let fileExtension: CFString = ofType! as CFString
            guard
                let extUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
                                    fileExtension, nil)?.takeUnretainedValue()
            else { return }
    
            guard
                let mimeUTI: NSString = UTTypeCopyPreferredTagWithClass(extUTI,
                                    kUTTagClassMIMEType)?.takeRetainedValue()
            else { return }
    
            let mimeType: String = mimeUTI as String
    
            do {
                let data: Data = try NSData(contentsOfFile: bundlePath!) as Data
    
                //Create a NSURLResponse with the correct mimetype.
                let urlResponse = URLResponse(url: url, mimeType: mimeType,
                                              expectedContentLength: data.count, textEncodingName: "utf8")
                urlSchemeTask.didReceive(urlResponse)
                urlSchemeTask.didReceive(data)
                urlSchemeTask.didFinish()
            }  catch _ as NSError {
                return
            }
    
        }
    
        func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
        }
    }
    

提交回复
热议问题