preface

There are currently four ways to preview a document on iOS:

UIWebView (soon to be scrapped by Apple) or WKWebView

2, QLPreviewController

3, UIDocumentInteractionController

4, CGContexDrawPDFPage

The project is currently using the first way, and the use of WKWebView to preview the document, I in iOS14 before the official release, spent a lot of energy to test the project in advance, found a weird problem, whether QQ, UC browser, QQ browser can not display PDF, Word and other documents, and the content is displayed empty, Only a few documents are displayed properly.

Load document-related Swift code

let url = NSURL.fileURL(withPath: self.filePath ?? "")
self.webView.load(URLRequest.init(url: url));
Copy the code

Where, self.filePath is the local path of the document. After checking, it is found that the document exists, but the page keeps showing blank. At the same time, it is found that the document name in the path contains Chinese string, so it cannot be displayed normally.

To solve

After MD5 processing on the name of the document, a string that does not contain Chinese is obtained. After the document is copied to the specified path, the document will be displayed normally after WKWebView loads it.

MD5 code attached:

- (NSString *)md5String {
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(self.bytes, (CC_LONG)self.length, result);
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
Copy the code