1

I would like my iOS app to host a small HTTP server, such that other app users can download files from the host.

I've tried using both Telegraph and Swifter. I can get the server to work on localhost but I can't figure out how to

  1. Get the device's IP where the server is hosted
  2. Configure it to host publicly at this IP

Telegram Code

let server = Server()
try! server.start(port: 9000)
    
let demoBundleURL = Bundle.main.url(forResource: "Demo", withExtension: nil)!
server.serveDirectory(demoBundleURL, "/demo")
    
server.route(.GET, "status") { (.ok, "Server is running") }

SWIFTER CODE

let server = HttpServer()
server["/"] = scopes {
  html {
    body {
      center {
        img { src = "https://swift.org/assets/images/swift.svg" }
      }
    }
  }
}
server["/files/:path"] = directoryBrowser("/")

let semaphore = DispatchSemaphore(value: 0)
do {
  try server.start(9080, forceIPv4: true)
  print("Server has started ( port = \(try server.port()) ). Try to connect now...")
  semaphore.wait()
} catch {
  print("Server start error: \(error)")
  semaphore.signal()
}
Anters Bear
  • 1,816
  • 1
  • 15
  • 41
  • server is hosted local. you can access it at `localhost` ip address - `127.0. 0.1` . – Blind Ninja Feb 04 '21 at 05:05
  • I would like to make a public server that anyone on the web can access. Is this possible? – Anters Bear Feb 04 '21 at 05:07
  • as long as your device has static ip it is possible but I am not sure carriers around the globe provide static IPs to mobile devices. you will need something in middle for latest ip. – Blind Ninja Feb 04 '21 at 06:24
  • I think it is better to find the ip device that is to be hosted first and then send it to others using BLE or nFc. The link below may help you https://stackoverflow.com/questions/30748480/swift-get-devices-wifi-ip-address – hessam Feb 04 '21 at 06:41
  • showmyip.com or similar services. – Erik Kaplun Oct 22 '22 at 10:27

0 Answers0