MGWebServer is a lightweight, cross-platform HTTP server framework for iOS, iPadOS, and macOS. It supports Bonjour service discovery, background task management, and audio playback to keep the server alive on iOS and iPadOS.
- Cross-platform support for iOS, iPadOS, and macOS
- Bonjour service discovery
- Background task management
- Audio playback to keep the server alive on iOS and iPadOS
- Easily add routes to handle HTTP requests
- Static file serving
To integrate MGWebServer into your project using Swift Package Manager, add the following dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/mortgy/MGWebServer.git", from: "1.0.0")
]Then, in your target, add MGWebServer as a dependency:
targets: [
.target(
name: "YourApp",
dependencies: ["MGWebServer"]
)
]- AppDelegate for iOS/iPadOS
import UIKit
import MGWebServer
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var webServer: MGWebServer?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let configuration = MGWebServerConfiguration(port: 8080, enableKeepAlive: true, enableBonjour: true)
webServer = MGWebServer(configuration: configuration)
webServer?.addRoute(path: "/hello") { request in
return HTTPResponse(statusCode: 200, headers: ["Content-Type": "text/plain"], body: "Hello, world!".data(using: .utf8))
}
webServer?.start()
return true
}
func applicationWillTerminate(_ application: UIApplication) {
webServer?.stop()
}
}- AppDelegate for macOS
import Cocoa
import MGWebServer
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var webServer: MGWebServer?
func applicationDidFinishLaunching(_ aNotification: Notification) {
let configuration = MGWebServerConfiguration(port: 8080, enableKeepAlive: true, enableBonjour: true)
webServer = MGWebServer(configuration: configuration)
webServer?.addRoute(path: "/hello") { request in
return HTTPResponse(statusCode: 200, headers: ["Content-Type": "text/plain"], body: "Hello, world!".data(using: .utf8))
}
webServer?.start()
}
func applicationWillTerminate(_ aNotification: Notification) {
webServer?.stop()
}
}To enable Bonjour service in your project, you need to add the following settings to your project's Info.plist file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>_mg-http._tcp</string>
</array>
</plist>- ViewController for iOS/iPadOS/macOS
import UIKit
import MGWebServer
class ViewController: UIViewController {
private let bonjourClientManager = BonjourClientManager()
override func viewDidLoad() {
super.viewDidLoad()
// Start browsing for services
bonjourClientManager.startBrowsing { services in
for service in services {
print("Discovered service: \(service.name), URL: \(service.url), IP: \(service.ipAddress), Device Type: \(service.deviceType)")
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Stop browsing for services when the view disappears
bonjourClientManager.stopBrowsing()
}
}The core server class that manages the HTTP server, routes, background tasks, and Bonjour services.
Configuration class for initializing MGWebServer with various options.
Manages publishing the server's URL and port over Bonjour.
Manages discovering Bonjour services on the local network.
Handles background task management to keep the server alive on iOS and iPadOS.
Handles audio playback to keep the server alive on iOS and iPadOS.
Classes for managing HTTP requests and responses.
- iOS 12.0+ / macOS 10.14+ / visionOS 1.0+
- Xcode 11+
- Swift 5.1+
MGWebServer is available under the MIT license. See the LICENSE file for more info.