2

I am not accessing from a different network. I am just trying to serve files for a hybrid app.

Just running the basic server works as intended when I am connected to a wifi network using the code below:

  let webServer = GCDWebServer()

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

    })

    webServer.startWithPort(8080, bonjourName: "GCD Web Server")

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

logs:

Visit http://192.168.1.132:8080/ in your web browser

When I drop wifi and start the app and server I get:

GCDWebServer started on port 8080 and reachable at (null)

yeahdixon
  • 6,647
  • 1
  • 41
  • 43
  • Does your device have a 3G/4G connection? If not, then when WiFi is off it has no active interfaces. In theory, the loopback interface should still be active, but GCDWebServer doesn't seem to have an option to allow you to specify an interface – Paulw11 Jul 15 '16 at 01:24
  • Good point . No 3g/4g on the device. – yeahdixon Jul 15 '16 at 02:33
  • Suggest you rename title of post to use GCDWebServer for better exposure for this Q/A – DwarDoh Aug 23 '17 at 20:08

4 Answers4

3

Try starting the server binding to localhost regardless of connection type (wifi or 3g/4g)

do {
    try webServer.start(options: [
        "Port": 8080,
        "BindToLocalhost": true
        ])
} catch {
    // handle error
}

Also, if Arbitrary Loads are disabled (probably), add "localhost" to NSExceptionDomains in info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
</dict>

enter image description here

Omer Faruk Ozturk
  • 1,722
  • 13
  • 25
  • 1
    This saved my life loading a webpage in a webview from local. It crashed when not in WiFi but this solved it. – juanjovn May 22 '21 at 11:20
1

serverUrl is null, but good ol local host works :

localhost://

Also available was the bonjour service url that is served up

func webServerDidCompleteBonjourRegistration(server: GCDWebServer!) {

    if(self.serverURL == nil){
        self.serverURL=self.webServer!.bonjourServerURL
        print(self.serverURL)
        self.initWebView()
    }


}

that got things running locally with wifi off.

yeahdixon
  • 6,647
  • 1
  • 41
  • 43
1

This is expected behavior although the docs could be more clear on this.

- (NSURL*)serverURL is just a convenience method that returns the primary IP address of your device appended with the server port. If your device is not connected to a network, it will return nil. See the implementation of GCDWebServerGetPrimaryIPAddress().

If this method returns returns nil, it simply means either the server is not running or it doesn't appear to have an external facing IP i.e. it's only reachable through localhost.

Pol
  • 3,848
  • 1
  • 38
  • 55
0

Using the URL http://127.0.0.1:8080/ was the only option that worked for me when not on wifi.

neave
  • 2,482
  • 1
  • 26
  • 18