iOS WKWebview loadHTMLString(_ baseURL:) 加载图片和读取css失败 [英] iOS WKWebview loadHTMLString(_ baseURL:) fails to load images and read css

查看:52
本文介绍了iOS WKWebview loadHTMLString(_ baseURL:) 加载图片和读取css失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过这个问题,但我的情况有点不同.

I know this question has been asked before but I have a little different situation.

我正在尝试调用 WKWebview 上的 loadHTMLString(_ baseURL:) 并且我的 baseURL 位于 AppData/Documents 目录中.

I'm trying to call to loadHTMLString(_ baseURL:) on WKWebview and my baseURL is in the AppData/Documents directory.

我需要加载 HTMLstring 并且它需要使用该目录中的图像.HTMLstring 不在任何地方,我下载字符串解析它,修改它并加载它.

I need the HTMLstring to load and it needs to use the images in that directory. the HTMLstring is not located anywhere, I download the string parse it, modify it and load it.

据我所知,由于某种安全问题(它在 UIWebview 上运行良好),我不能这样做,所以 HTMLstring 已加载,但图像和 CSS 不是.

As I understand I can't do that because of some sort of security issue (it worked great on UIWebview), so the HTMLstring is loaded but the images and CSS are not.

另外,我无法调用 loadFileURL(_, allowedReadAccessTo:) 因为我没有文件我有一个 HTMLstring 并且无法保存它由于我自己的安全问题,在通话前提交.

Also, I can't call for loadFileURL(_, allowingReadAccessTo:) because I don't have a file I have an HTMLstring and can't save it to file just before the call, because of my own security issues.

我的代码很大而且很复杂,我想以最轻松的方式实现加载图像.

My code is huge and has a lot of complexity I would like to achieve loading images in the most painless way.

有人有想法吗?

推荐答案

我希望我有更好的答案,如果有人有更好的答案,我会很乐意接受.我最终将我的文件移动到临时目录:( 我已经使用了这个 -

I wish I had a better answer and if anybody has a better answer I will gladly accept it. I ended up moving my files to the temp directory :( I've used this -

extension FileManager
{
    func copyToTemporaryDirectory(itemIn itemURL: URL) -> URL?
    {
        let tempDirectoryURL    = URL(fileURLWithPath: NSTemporaryDirectory())
        let fileName            = itemURL.lastPathComponent
        let copyURL             = tempDirectoryURL.appendingPathComponent(fileName)
        let fileManager         = FileManager.default

        do {

            if fileManager.fileExists(atPath: copyURL.path)
            {
                try fileManager.removeItem(at: copyURL)
            }

            try FileManager.default.copyItem(at: itemURL, to: copyURL)

            return copyURL

        }
        catch let error
        {
            logger.debug("copyItem error: \(error.localizedDescription)")
            return nil
        }
    }
}

然后 loadHtmlStringURL 到这个目录.从此目录中,WKWebview 可以获取所有内容.

and then loadHtmlString with the URL to this directory. from this directory, WKWebview is allowed to get everything.

这篇关于iOS WKWebview loadHTMLString(_ baseURL:) 加载图片和读取css失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆