2015-10-04 195 views
1

我嘗試運行GCDWebServer以便從它要使用的相同設備訪問它。GCDWebServer 500錯誤

我有下面的代碼

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") 
    } 

在控制檯我有以下輸出

enter image description here

如何使它工作?

回答

0

日誌表明您的HTTP請求無效,因此500錯誤。

確保傳遞給-loadRequest的URL有效(例如,在index.html之前可能會丟失/)。