gpt4 book ai didi

swift Playground : Rendering Local html file in WebKit works on simulator but not on device

转载 作者:行者123 更新时间:2023-11-30 10:48:15 25 4
gpt4 key购买 nike

我正在尝试使用 Playground 上的本地 HTML 文件来渲染 WebKit View ,但它会在模拟器上渲染它,但不会在 Playground 上渲染。

我的代码看起来像这样:

import UIKit
import Foundation
import PlaygroundSupport
import WebKit

public class Plastic : UIViewController, WKUIDelegate {

var webView: WKWebView!

public override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}

public override func viewDidLoad() {
super.viewDidLoad()

let url = Bundle.main.url(forResource: "plastic", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let myRequest = URLRequest(url: url)
webView.load(myRequest)
}

@objc func notification(){
PlaygroundPage.current.liveView = Banana()
}

}

这可能是 Playgrounds 中导致本地 HTML 文件无法渲染的错误吗?这对我来说是奇怪的行为,我不知道如何解决它。

最佳答案

Bodge 的艺术。这是我修复它以在设备上工作的一种方法。这不是最好的方法,但它在我的短时间内完成了工作。

if let url = Bundle.main.path(forResource: "pla", ofType: "html"){
do{
let contents = try String(contentsOfFile: url)
webView.loadHTMLString(contents, baseURL: nil)
} catch {
return
}

}

关于 swift Playground : Rendering Local html file in WebKit works on simulator but not on device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com