I have the following code, which is basically a template of a blank app that Xcode created, and I'm using a library called Swifter (renamed here Swifter2 because of name conflict) where Im trying to start a local http server on my iPhone device. This was my attempt to do so:
//
// ContentView.swift
// HTTPServerIOSApp
import SwiftUI
import Swifter2
func startServer() {
do {
let server = HttpServer()
server["/"] = { request in
return HttpResponse.ok(.text("<html string>"))
}
try server.start(9081)
} catch {
print("Server start error: \(error)")
}
}
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
Button("Start Server!",
action: {
startServer()
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The reason why Im using a button to call the function is because apparently (Im very new to Swift so this isn't clear to me) I couldn't just call the function in the ContentView. Now the problem is that when I call the function it seems to work but I can't find anything on the localhost:9081, it keeps saying the page doesn't exist. Is it because the application doesn't run in background and thus doesn't keep the server alive ?