Swiftpack.co - yene/GCDWebServer as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by yene.
yene/GCDWebServer 3.5.7
Turned GCDWebServer into a Swift Package.
⭐️ 18
🕓 17 weeks ago
iOS macOS
.package(url: "https://github.com/yene/GCDWebServer.git", from: "3.5.7")

Swift Package of GCDWebServer

based on swisspol/GCDWebServer.

GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in iOS, macOS & tvOS apps.

Using it

  1. Open File > Add Package
  2. Paste the URL into top right search https://github.com/yene/GCDWebServer
  3. Extend your project, for example start the server in your didFinishLaunchingWithOptions
import UIKit
import GCDWebServer

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  let webServer = GCDWebServer()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
      return GCDWebServerDataResponse(html:"<html><body><p>Hello World</p></body></html>")
    })
    webServer.start(withPort: 8080, bonjourName: "GCD Web Server")
    print("Visit \(webServer.serverURL) in your web browser")
    return true
  }
  // ...
}

Serving Static Directory

Drag the folder containing your html files into the Xcode project. In this example the folder is called dist and has an index.html file inside.

During development make sure to disable cache.

import UIKit
import GCDWebServer

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
	let webServer = GCDWebServer()

	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

		let subdir = Bundle.main.resourceURL!.appendingPathComponent("dist").path
		webServer.addGETHandler(forBasePath: "/", directoryPath: subdir, indexFilename: "index.html", cacheAge: 3600, allowRangeRequests: true)
		webServer.start(withPort: 8080, bonjourName: "GCD Web Server")
		print("Visit \(webServer.serverURL) in your web browser")
		return true
	}
  // ...
}

GitHub

link
Stars: 18
Last commit: 17 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v3.5.7
17 weeks ago
  • Copy PrivacyInfo.xcprivacy into bundle

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics