我正在 Switft Playground 工作,并试图让 WebView 显示完整的 HTML5 内容,包括图像。目前,如果它来自网络,我可以让 WebView 显示完整的 HTML5 内容,但如果它是从 Playground 本地加载的,则只能显示文本。
这是我来源的代码:
import Foundation
import WebKit
import Foundation
import UIKit
public class MyWebKitViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
public override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
public func loadLocalHtml(filename: String) {
let possiblePath = Bundle.main.path(forResource: filename, ofType: "html")
if let path = possiblePath {
let url = URL(fileURLWithPath: path)
webView.loadFileURL(url, allowingReadAccessTo: url)}
}
}
这是我用来调用 loadLocalHtml 函数的代码:
import UIKit
import PlaygroundSupport
//Set current Playground View
let vc = MyWebKitViewController()
PlaygroundPage.current.liveView = vc
vc.loadLocalHtml(filename: "testing")
我已经尝试过其他人针对类似问题发布的解决方案,但没有成功。在某些情况下,我得到的 WebView 仅显示白色背景但根本没有内容。在此先感谢您的帮助!
最佳答案
同样的问题。应该是这样的,但我不明白。看起来 Swift Playgrounds 有不同的方式来管理资源。在 Xcode Playground 中,您的代码按预期工作
import UIKit
import WebKit
import PlaygroundSupport
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 375, height: 667))
if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
let imageURL = Bundle.main.url(forResource: "image1", withExtension: "png", subdirectory: "images/")
webView.loadFileURL(url, allowingReadAccessTo: imageURL!)
}
PlaygroundPage.current.liveView = webView
如果你找到办法,请回答:]
关于Swift Playground WebView 不会显示本 map 片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47932266/