WKWebView:jquery数据表CSV导出

时间:2016-12-14 11:39:01

标签: jquery ios datatables export-to-csv wkwebview

在我的iOS应用程序中,我使用WKWebView显示带有CSV按钮的jquery数据表以导出数据(页面非常类似于https://datatables.net/extensions/buttons/examples/initialisation/export.html)。现在,当我点击CSV按钮时,我会处理数据以便在用户处显示,例如QLPreviewController。在iOS Safari浏览器上,点击CSV按钮时,会打开另一个带有数据的选项卡。在我WkWebView没有任何反应。

有没有人可以帮助我实现目标?

1 个答案:

答案 0 :(得分:0)

我自己解决了。 我可以通过optional func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?

WKUIDelegate方法处理点击

编辑 - 在评论中添加了@SeriousSam所要求的代码

// MARK: - WKUIDelegate

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    if navigationAction.targetFrame == nil {
       var urlString = navigationAction.request.url
       let lastPathComponent = urlString!.lastPathComponent
       let encodedLastPathComponent = lastPathComponent.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
       urlString?.deleteLastPathComponent()
       let encodedUrl = urlString!.absoluteString+encodedLastPathComponent!
       let jsHandler = "var xhr = new XMLHttpRequest;xhr.responseType = 'blob';xhr.onload = function() {var recoveredBlob = xhr.response;var reader = new FileReader;reader.onload = function() {var blobAsDataUrl = reader.result;window.location = blobAsDataUrl;};reader.readAsDataURL(recoveredBlob);};xhr.open('GET', '\(encodedUrl)');xhr.setRequestHeader(\"Cache-Control\", \"no-store\");xhr.send();"
       webView.evaluateJavaScript(jsHandler, completionHandler: nil)
    }
    return nil
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if let url = navigationAction.request.url {
        if (url.absoluteString.hasPrefix("data:")) {
            let splitted = url.absoluteString.components(separatedBy: "base64,")
            if splitted.count == 2 {
                if splitted[0].contains("csv") {
                    self.documentName = "export.csv"
                }
                if let data = Data(base64Encoded: splitted[1]), let documentName = self.documentName {
                    let fileName = String(format: "%@.%@", ProcessInfo.processInfo.globallyUniqueString, "\((documentName as NSString).pathExtension)")
                    let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
                    do {
                        try data.write(to: fileURL, options: .atomicWrite)
                        // show the CSV document ...
                        decisionHandler(.cancel)
                        return
                    } catch {
                        let alert = UIAlertController(title: NSLocalizedString("Errore", comment: "Error Alert Title"), message: error.localizedDescription, preferredStyle: .alert)
                        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
                        self.present(alert, animated: true, completion: nil)
                        decisionHandler(.cancel)
                        return
                    }
                }
            }
        }
    }
    decisionHandler(.allow)
}
相关问题