iOS GCDWebServer HTTPServer Localhost ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

iOS GCDWebServer HTTPServer Localhost ?

mitchboo
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: iOS GCDWebServer HTTPServer Localhost ?

Sam Stigler
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


On Nov 22, 2014, at 2:27 PM, [hidden email] wrote:

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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/53881127-FED2-4F5E-BBDC-7B86577DBC35%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: iOS GCDWebServer HTTPServer Localhost ?

mitchboo
In reply to this post by mitchboo
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 :(


On Saturday, November 22, 2014 9:27:08 PM UTC+1, [hidden email] wrote:
I am trying to understand how to use the Swift GCDWebServer in my app to create a localhost server from the project at
<a href="https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/7f46d37d-dbc8-4464-8c85-644473a191b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: iOS GCDWebServer HTTPServer Localhost ?

Steve McIntosh
In reply to this post by mitchboo
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()

    }

}



On Sunday, 23 November 2014 07:27:08 UTC+11, [hidden email] wrote:
I am trying to understand how to use the Swift GCDWebServer in my app to create a localhost server from the project at
<a href="https://github.com/swisspol/GCDWebServer" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/fb9ca78b-39b0-4349-92d3-99ce6df728e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: iOS GCDWebServer HTTPServer Localhost ?

mitchboo
Thank you Steve. In which module do you place that ?

On Tuesday, December 9, 2014 7:04:09 AM UTC+1, Steve McIntosh wrote:
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()

    }

}



On Sunday, 23 November 2014 07:27:08 UTC+11, [hidden email] wrote:
I am trying to understand how to use the Swift GCDWebServer in my app to create a localhost server from the project at
<a href="https://github.com/swisspol/GCDWebServer" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3a388d7a-0f66-4690-bdae-322f30751bc7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: iOS GCDWebServer HTTPServer Localhost ?

Andrew Torney
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

On Tuesday, 9 December 2014 10:42:13 UTC, [hidden email] wrote:
Thank you Steve. In which module do you place that ?

On Tuesday, December 9, 2014 7:04:09 AM UTC+1, Steve McIntosh wrote:
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()

    }

}



On Sunday, 23 November 2014 07:27:08 UTC+11, [hidden email] wrote:
I am trying to understand how to use the Swift GCDWebServer in my app to create a localhost server from the project at
<a onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fswisspol%2FGCDWebServer\46sa\75D\46sntz\0751\46usg\75AFQjCNEui_zlIQ6493Kj7R1EKOhK_j_TWg';return true;" href="https://github.com/swisspol/GCDWebServer" rel="nofollow" target="_blank">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8fbb811a-d2b3-4e34-8ec8-324293b02264%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.