Hello,
This renders a screen with strange characters.
When trying to load a different file type (eg. PDF, PNG, JPEG), it works perfectly.
Also tried loading the .doc data into a UIWebview and it also works fine. I am getting the data from a server using JSON encoded to Base64.
let webview = WKWebView()
webview.load(data, mimeType: "application/msword", characterEncodingName: "UTF-8", baseURL: NSURL() as URL)
Has anyone encountered this issue as well?
File should not save locally.
it is condition.
I’m not entirely sure what’s going on here, and you should definitely file a bug about this, especially as
UIWebView
handles it correctly. Please post your bug number, just for the record.
Personally I’d work around this by writing the data to disk and then loading it using
loadFileURL(_:allowingReadAccessTo:)
. However, if your not allowed to do that then you’ll have to explore other options. I managed to get this working using a
data
URL. For example:
let docURL = Bundle.main.url(forResource: "Test", withExtension: "doc")!
let docContents = try! Data(contentsOf: docURL)
let urlStr = "data:application/msword;base64," + docContents.base64EncodedString()
let url = URL(string: urlStr)!
let request = URLRequest(url: url)
self.webView.load(request)
IMPORTANT I can’t guarantee that
WKWebView
doesn’t write this data to disk internally. Which makes a bit of a mockery of the whole “file should not save locally” requirement.
Note I’ve gone against best practice here and built the URL using string primitives rather than
URLComponents
. That’s required because if you use
URLComponents
the semicolon in the
path
property gets encoded as
%3B
. I believe that
URLComponents
is behaving correctly here, but this encoding isn’t supported by
WKWebView
.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"