Swift Tutorial to add GCDWebServer into XCode Project

GCDWebServer is a former Objective-C project that embeds a web server in iOS / OSX application. This tutorial explains how to add GCDWebServer into Swift based iOS project.

Step 1:

Navigate to empty directory, run the following

git clone https://github.com/swisspol/GCDWebServer

Step 2:

Go into GCDWebServer directory, you will see there are GCDWebServer, GCDWebDAVServer, GCDWebUploader…. In this case, we just need GCDWebServer to build a simple HTTP Server. Copy this folder to your application root folder. Looks like this.

Screen Shot 2014-12-23 at 2.20.52 pm

Step 3:

Add a bridging header if it is not exist.

Right click your project navigator, add a new “header file” under iOS source tab. Name it as <YourApplicationName>-Bridging-Header.h.

In this file, put the following

#import "GCDWebServer.h"
#import "GCDWebServerDataResponse.h"

Step 4:

Go to your project navigator, build settings, Swift compiler – Code Generation, Objective-C Bridging Header, enter the path to the <YourApplicationName>-Bridging-Header.h

Screen Shot 2014-12-23 at 2.25.47 pm

Step 5:

Go to your project navigator, build phases, under Link Binary with Libraries, click to add “libz.dylib”.

Step 6:

At the time of writing, the current GCDWebServer not working properly and have to change something make it to work.

Find GCDWebServer.h and locate addDefaultHandlerForMethod, set the second one as addDefaultAsyncHandlerForMethod.

Find GCDWebServer.m and locate addDefaultHandlerForMethod, set the second one as addDefaultAsyncHandlerForMethod. Under first addDefaultHandlerForMethod, change the second line of the occurrence to addDefaultAsyncHandlerForMethod instead.

Screen Shot 2014-12-23 at 2.33.40 pm

Step 7:

Put following test code and run.

// http server

        let webServer = GCDWebServer()


        webServer.addDefaultHandlerForMethod(“GET”, requestClass: GCDWebServerRequest.self) { request in

            return GCDWebServerDataResponse(HTML:“<html><body><p>Hello World</p></body></html>”)


        webServer.startWithPort(8080, bonjourName: nil)

        println(“Visit \(webServer.serverURL) in your web browser”)

Step 8:

Open your desktop browser, and open the link shown in debug console. You should be able to see Hello World displayed in the web page.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.