|
1 | 1 | # Swift Package of GCDWebServer |
2 | | -Turned GCDWebServer into a Swift Package. |
3 | | -based on https://github.com/swisspol/GCDWebServer |
| 2 | +based on [swisspol/GCDWebServer](https://github.com/swisspol/GCDWebServer). |
| 3 | + |
| 4 | +> GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in iOS, macOS & tvOS apps. |
| 5 | +
|
| 6 | +## Hello World Example |
| 7 | +Open File > Add Package and |
| 8 | +Paste the URL into top right search `https://github.com/yene/GCDWebServer` |
| 9 | + |
| 10 | +```Swift |
| 11 | +import UIKit |
| 12 | +import GCDWebServer |
| 13 | + |
| 14 | +@main |
| 15 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 16 | + let webServer = GCDWebServer() |
| 17 | + |
| 18 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 19 | + webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in |
| 20 | + return GCDWebServerDataResponse(html:"<html><body><p>Hello World</p></body></html>") |
| 21 | + }) |
| 22 | + webServer.start(withPort: 8080, bonjourName: "GCD Web Server") |
| 23 | + print("Visit \(webServer.serverURL) in your web browser") |
| 24 | + return true |
| 25 | + } |
| 26 | + // ... |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Serving Static Directory |
| 31 | +Drag the folder containing your html files into the Xcode project. |
| 32 | +In this example the folder is called `dist` and has an `index.html` file inside. |
| 33 | + |
| 34 | +> During development make sure to disable cache. |
| 35 | +
|
| 36 | +```Swift |
| 37 | +import UIKit |
| 38 | +import GCDWebServer |
| 39 | + |
| 40 | +@main |
| 41 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 42 | + let webServer = GCDWebServer() |
| 43 | + |
| 44 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 45 | + |
| 46 | + let subdir = Bundle.main.resourceURL!.appendingPathComponent("dist").path |
| 47 | + webServer.addGETHandler(forBasePath: "/", directoryPath: subdir, indexFilename: "index.html", cacheAge: 3600, allowRangeRequests: true) |
| 48 | + webServer.start(withPort: 8080, bonjourName: "GCD Web Server") |
| 49 | + print("Visit \(webServer.serverURL) in your web browser") |
| 50 | + return true |
| 51 | + } |
| 52 | + // ... |
| 53 | +} |
| 54 | + |
| 55 | +``` |
0 commit comments