Skip to content

Instantly share code, notes, and snippets.

@helje5
Created May 22, 2025 11:55
Show Gist options
  • Save helje5/0224f2bd5f8944fc996ad486fa6ac69e to your computer and use it in GitHub Desktop.
Save helje5/0224f2bd5f8944fc996ad486fa6ac69e to your computer and use it in GitHub Desktop.
Macro driven SwiftUI App w/ WebView
import SwiftUI
import MacroExpress // @Macro-swift
import WebView // @kylehickinson
@main
struct WebViewApp: App {
@StateObject var state = WebViewStore()
let url = URL(string: "http://127.0.0.1:1337/")!
let app = Express()
.post("/upload", multer().array("file", 10)) { req, res, _ in
req.log.info("Got files:", req.files["file"])
res.send("Upload complete: \(req.files["file"] ?? [])")
}
.get { req, res in
res.setHeader("Content-Type", "text/html")
res.send(
"""
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
<input type="file" name="file">
<input type="submit" value="Upload" />
</form>
</body>
</html>
"""
)
}
var body: some Scene {
WindowGroup {
VStack {
WebView(webView: state.webView)
Divider()
HStack {
Button("Back", systemImage: "chevron.left") {
state.webView.goBack()
}
.disabled(!state.webView.canGoBack)
Spacer()
Button("Forward", systemImage: "chevron.right") {
state.webView.goForward()
}
.disabled(!state.webView.canGoForward)
}
.labelStyle(.iconOnly)
}
.padding()
.onAppear {
app.listen(url.port!) { app in
print("Listening!", app)
DispatchQueue.main.async {
state.webView.load(URLRequest(url: url))
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment