【问题标题】:WKWebView: jquery datatable CSV exportWKWebView:jquery 数据表 CSV 导出
【发布时间】:2016-12-14 11:39:01
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    我自己解决了。 我可以通过WKUIDelegateoptional func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?方法处理水龙头

    编辑 - 根据@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)
    }
    

    【讨论】:

    • 你能分享一下你实际处理它的代码sn-p吗?
    • @SeriousSam 编辑了答案。自从我写代码以来已经有一段时间了。无论如何,我希望它对你有用
    猜你喜欢
    • 2013-03-06
    • 2021-08-16
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多