我正在使用 aWKWebView
来提供index.html
单页网络应用程序(ember),如下所示:
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 中的捆绑包中。
我在这里想念什么?