gpt4 book ai didi

ios - 为什么我的 XWebView 使用文件方案提供 index.html,我该如何更改它?

转载 作者:行者123 更新时间:2023-11-28 06:57:25 25 4
gpt4 key购买 nike

我正在使用 WKWebView 来提供单页网络应用 (ember) 的 index.html,如下所示:

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let webview = WKWebView(
frame: view.frame,
configuration: WKWebViewConfiguration()
)
view.addSubview(webview)

let root = NSBundle.mainBundle().resourceURL!
let url = root.URLByAppendingPathComponent("dist/index.html")
webview.loadFileURL(url, allowingReadAccessToURL: root)

加载索引文件效果很好。但是索引文件正在使用文件方案请求它的链接和资源?如果我在检查器中运行时使用 Safari 检查应用程序,我会看到所有本地资源的错误:

[Error] Failed to load resource: ... file:///dist/assets/css/vendor.css

index.html 中看起来像:

<link rel="stylesheet" href="./dist/assets/vendor.css">

我想要的是资源请求转到我的 GCDWebServer我这样设置的:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var webServer: GCDWebServer?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.webServer = GCDWebServer()
self.webServer!.addGETHandlerForBasePath("/",
directoryPath: NSBundle.mainBundle().bundlePath,
indexFilename: nil,
cacheAge: 3600,
allowRangeRequests: true
)
self.webServer!.startWithPort(8080, bonjourName: "GCD Web Server")
print("GCD Server running at: \(self.webServer!.serverURL)")
return true

并且我已将 dist 文件夹添加到 Xcode 中的包中。

我在这里错过了什么?

最佳答案

这就是 HTML 的工作原理。不是绝对的 HREF 是相对于引用页面的来源的。所以如果你有 images/foo.pngfile:///dir/index.html , 浏览器会请求 file:///dir/images/foo.png .

如果您需要浏览器从不同位置获取资源,则需要在 HTML 中使用绝对 URL(例如 http://localhost:8080/whatever)。

关于ios - 为什么我的 XWebView 使用文件方案提供 index.html,我该如何更改它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059197/

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