Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我的XWebView服务index.html使用文件方案,以及如何更改它?_Ios_Swift_Wkwebview_Gcdwebserver - Fatal编程技术网

Ios 为什么我的XWebView服务index.html使用文件方案,以及如何更改它?

Ios 为什么我的XWebView服务index.html使用文件方案,以及如何更改它?,ios,swift,wkwebview,gcdwebserver,Ios,Swift,Wkwebview,Gcdwebserver,我正在使用WKWebView来提供单页web应用程序()的index.html,如下所示: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let webview = WKWebView( frame: view.frame, configuration: WKWebView

我正在使用
WKWebView
来提供单页web应用程序()的
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)
@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
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let serverURL = appDelegate.webServer!.serverURL
let indexURL = serverURL.URLByAppendingPathComponent("dist/index.html")
webview.loadRequest(NSURLRequest(URL: indexURL))
这可以很好地加载索引文件。但是索引文件正在使用文件方案请求它的链接和资源?如果我在inspector中运行时使用Safari检查应用程序,我会在所有本地资源中看到此错误:

[Error] Failed to load resource: ... file:///dist/assets/css/vendor.css
index.html
中,如下所示:

<link rel="stylesheet" href="./dist/assets/vendor.css">
我在Xcode中将
dist
文件夹添加到bundle中


我在这里遗漏了什么?

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


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

解决方案非常简单。我只需要使用
webview.loadRequest
而不是
webview.loadFileURL
从localhost为我的
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)
@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
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let serverURL = appDelegate.webServer!.serverURL
let indexURL = serverURL.URLByAppendingPathComponent("dist/index.html")
webview.loadRequest(NSURLRequest(URL: indexURL))

正如Andrew Medico指出的,
href
是相对于它们的父页面的,因此如果我从localhost提供
index.html
,那么我的所有资源也会从localhost请求。解决了

我是XWebView的作者。我不太明白你的要求。如果在iOS 8上使用XWebView支持文件URL,为什么要包括GCDWebServer?任何错误报告或问题,您都可以在github上提交问题通知单,以便我们尽最大努力支持您。thnx鉴于此,这里的问题不是XWebView,而是我滥用loadFileURL而不是loadRequest。见下面我的答案