【发布时间】:2019-09-03 16:22:39
【问题描述】:
一段时间以来,我一直在使用 UIWebView 在我的应用程序中显示 Microsoft Office 文档(Word、PowerPoint、Excel),但 Apple 最近弃用了 UIWebView 类。我正在尝试切换到 WKWebView,但 Word、Excel 和 Powerpoint 文档无法在 WKWebView 中正确呈现。
使用 UIWebView 显示 Excel 文档(效果很好):
let data: Data
//data is assigned bytes of Excel file
let webView = UIWebView()
webView.load(data, mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", textEncodingName: "UTF-8", baseURL: Bundle.main.bundleURL)
尝试使用 WKWebView 做同样的事情(显示一堆无意义的字符而不是 Excel 文件):
let data: Data
//data is assigned bytes of Excel file
let webView = WKWebView.init()
webView.load(data, mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", characterEncodingName: "UTF-8", baseURL: Bundle.main.bundleURL)
由于我用例的性质,出于安全原因,我无法将数据保存到磁盘,因此我无法使用这样的方法:
webView.loadFileURL(<#T##URL: URL##URL#>, allowingReadAccessTo: <#T##URL#>)
我也不能使用 QuickLook (QLPreviewController),因为它又需要一个 URL。
----------------------------------------------- - - - - - - - - 编辑 - - - - - - - - - - - - - - - - - ------------------------------------
我也知道这种通过字符串 URL 传递数据的方法,但除非有人能证明数据从未写入磁盘,否则我不能接受它作为答案:
let data: Data
//data is assigned bytes of Excel file
let webView = WKWebView.init()
let urlStr = "data:\(fileTypeInfo.mimeType);base64," + data.base64EncodedString()
let url = URL(string: urlStr)!
let request = URLRequest(url: url)
webView.load(request)
【问题讨论】:
-
你可以在office在线编辑器中复制过去吗?
-
你有没有想过这个问题?
标签: ios swift uiwebview wkwebview qlpreviewcontroller