我尝试运行 GCDWebServer 以便从它将工作的同一设备访问它。
我有以下代码
import UIKit
import Foundation
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webServer: GCDWebServer?
var webView: WKWebView?
let urlpath : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRectMake(0, 0, 300, 300))
webView?.navigationDelegate = self
view.addSubview(webView!)
initWebServer()
let urlString:String = "\(webServer!.serverURL)index.html"
let url = NSURL(string: urlString)!
webView!.loadRequest(NSURLRequest(URL: url))
}
func initWebServer() {
webServer = GCDWebServer()
webServer!.addGETHandlerForBasePath("/", directoryPath: urlpath as String, indexFilename: "index.html", cacheAge: 3600, allowRangeRequests: true)
webServer!.startWithPort(8080, bonjourName: "GCD Web Server")
print("Visit \(webServer!.serverURL) in your web browser")
}
在控制台中我有以下输出
如何使它工作?