Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

html - iOS 13 Swift 5 wkwebview - display image from documents directory into wkwebview real device

I have an app with downloaded images to be displayed in local html in WKWEBVIEW. Everything is working fine but it is not showing image on iPhone device

the html is

"

<img src="images/d35fb6a3-8a21-4196-9616-ad2c6db60669/fd21b894-38c0-42c5-aa69-a938abe40e4b2467857252325869136.png">

<img src="images/d35fb6a3-8a21-4196-9616-ad2c6db60669/c927a2a6-4ef0-481d-b27d-525cec7ed3814195490571216387044.png">

"

matching to this HTML it is to be displayed in a wkwebview which gets displayed perfectly on simulator with the following code

let baseURL = Functions.FileIO.contentFolderURL()
static func contentFolderURL() -> URL? {
            guard let folderURL = DataManager.shared.applicationDocumentsURL?.appendingPathComponent("content") else {
                return nil
            }
            
            do {
                let properties = try (folderURL as NSURL).resourceValues(forKeys: [URLResourceKey.isDirectoryKey])
                if let isDirectory = properties[URLResourceKey.isDirectoryKey] as? Bool , isDirectory == false {
                    return nil
                }
            } catch let error as NSError where error.code != NSFileReadNoSuchFileError {
                return nil
            } catch {
                // No folder, so we create it.
                do {
                    try FileManager.default.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
                } catch {
                    return nil
                }
            }
            
            return folderURL
        }

and then finally displaying

baseURL comes to be

Optional ? some : file:///Users/paza/Library/Developer/CoreSimulator/Devices/D2204E03-8A8F-4EF4-8924-683CF519DD19/data/Containers/Data/Application/E4ED56CF-B247-4471-9F9B-23384FD6D6B3/Documents/content/ - _url : file:///Users/paza/Library/Developer/CoreSimulator/Devices/D2204E03-8A8F-4EF4-8924-683CF519DD19/data/Containers/Data/Application/E4ED56CF-B247-4471-9F9B-23384FD6D6B3/Documents/content/

self.webView?.loadHTMLString(HTML, baseURL: baseURL)

working in simulator below does not work in Device

as https://stackoverflow.com/a/52838445/3455426[ ]2

suggested I tried

self.webView?.loadFileURL(baseURL, allowingReadAccessTo: baseURL)

but the result this comes blank on real device

any leads will be appreciated , Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In WKWebview if we have to show HTML with Cached resources ,firstly we have to save the html string in HTML file in documents directory.

make sure ur cached resources path and the path of HTML file are same also , the cached resources are identified by the lastPathComponent only .

So the fault was the HTML which should have been

let HTML = "<html><body>
<p><img src="fd21b894-38c0-42c5-aa69-a938abe40e4b2467857252325869136.png"></p>
<p><img src="c927a2a6-4ef0-481d-b27d-525cec7ed3814195490571216387044.png"><br></p>
</body></html>"

Specifically the last path component should be there .

Also I followed https://en.it1352.com/article/9f185d4d185243cc92128148443ca660.html here the explanation is almost perfect , I just saved in documents directory for swift.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...