iOS GCDWebServer HTTPServer Localhost ?

1,588 views
Skip to first unread message

mitc...@gmail.com

unread,
Nov 22, 2014, 3:27:08 PM11/22/14
to swift-l...@googlegroups.com
I am trying to understand how to use the Swift GCDWebServer in my app to create a localhost server from the project at
https://github.com/swisspol/GCDWebServer

My issue is that I do not see where to paste the Hello World snippet the author posted on the page :

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

@interface AppDelegate : NSObject <UIApplicationDelegate> {
 
GCDWebServer* _webServer;
}
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

 
// Create server
  _webServer
= [[GCDWebServer alloc] init];

 
// Add a handler to respond to GET requests on any URL
 
[_webServer addDefaultHandlerForMethod:@"GET"
                           
requestClass:[GCDWebServerRequest class]
                           
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {

   
return [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];

 
}];

 
// Start server on port 8080
 
[_webServer startWithPort:8080 bonjourName:nil];
 
NSLog(@"Visit %@ in your web browser", _webServer.serverURL);

 
return YES;
}

@end

I tried to paste into ViewControler.swift, but it is full or red dots !

I will appreciate step by step guidance. This is probably rather simple for advanced users, it still baffles me. Please be kind to the less taught willing to learn.

Thank you in advance.

Sam Stigler

unread,
Nov 22, 2014, 3:31:55 PM11/22/14
to mitc...@gmail.com, swift-l...@googlegroups.com
I haven't taken a look at that project, but it seems like a good first step would be to paste the code into the right file: Since it's the AppDelegate class, it stands to reason it should go in AppDelegate.m .

Something important to note here is that the code you've pasted in is Objective-C source code, and it sounds like you're trying to paste it into a Swift file. Can you please clarify this?

Sam
--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/1b68090d-9283-40ea-9d2d-3fa304187c3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mitc...@gmail.com

unread,
Nov 23, 2014, 4:14:08 PM11/23/14
to swift-l...@googlegroups.com
You are right. I did not post the right code. Thank you.

When I add the following code, I get an error saying GCDWebServer is unknown, although following the author's instructions I added it to the project.

import Foundation

let webServer = GCDWebServer()

webServer
.addDefaultHandlerForMethod("GET", requestClass: GCDWebServerRequest.self) { request in
   
return GCDWebServerDataResponse(HTML:"<html><body><p>Hello World</p></body></html>")
}

webServer
.runWithPort(8080, bonjourName: nil)

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

Unfortunately, since the author does not seem to believe in posting anything but pictures and video, but no example projects, it is hopeless for the moment.

Looks like no help is in sight. Maybe time to give up :(

Steve McIntosh

unread,
Dec 9, 2014, 1:04:09 AM12/9/14
to swift-l...@googlegroups.com
I'm new to swift, but here is some code that works...

import Cocoa


class HTTPManager: GCDWebServer {


    // MARK: Properties

    var httpManager: HTTPManager!

    

    class var sharedInstance : HTTPManager {

        struct Static {

            static let instance : HTTPManager = HTTPManager()

        }

        return Static.instance

    }


    // MARK:

    deinit {

        // perform the deinitialization

        NSNotificationCenter.defaultCenter().removeObserver(self)

    }

    

    override init() {

        super.init()

    }

    

    // MARK: Setup

    func setup() {

        httpManager = HTTPManager.sharedInstance


        httpManager.addDefaultHandlerForMethod(NSString(UTF8String: "GET"), requestClass:GCDWebServerRequest.self, processBlock: {

            request in

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

        })

    }

    

    // MARK: Start/Stop

    override func start() -> Bool {

        return httpManager.runWithPort(8080, bonjourName:"BoilerPlate")

    }

    

    override func stop() {

        super.stop()

mitc...@gmail.com

unread,
Dec 9, 2014, 5:42:13 AM12/9/14
to swift-l...@googlegroups.com
Thank you Steve. In which module do you place that ?

Andrew Torney

unread,
Feb 24, 2015, 2:47:44 PM2/24/15
to swift-l...@googlegroups.com
The problem with the red dots is missing linkers. You have to add the following frameworks and libraries on your projects general tab.
 
libz.dylib
libxml2.dylib
CFNetwork
UIKit
MobileCoreServices
 
Hope this helps
Reply all
Reply to author
Forward
0 new messages