View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vidéos

Ouvrir le menu Fermer le menu
  • Collections
  • Toutes les vidéos
  • À propos

Plus de vidéos

  • À propos
  • Code
  • Use Xcode for server-side development

    Discover how you can create, build, and deploy a Swift server app alongside your pre-existing Xcode projects within the same workspace. We'll show you how to create your own local app and test endpoints using Xcode, and explore how you can structure and share code between server and client apps to ease your development process

    Ressources

    • Swift on Server
      • Vidéo HD
      • Vidéo SD

    Vidéos connexes

    WWDC23

    • Meet Swift OpenAPI Generator

    WWDC22

    • Meet distributed actors in Swift

    WWDC21

    • Meet async/await in Swift
    • Protect mutable state with Swift actors

    WWDC20

    • Use Swift on AWS Lambda with Xcode

    WWDC19

    • Creating Swift Packages
  • Rechercher dans cette vidéo…
    • 1:36 - Simple, server package manifest

      // swift-tools-version: 5.7
      
      import PackageDescription
      
      let package = Package(
          name: "MyServer",
          platforms: [.macOS("12.0")],
          products: [
              .executable(
                  name: "MyServer",
                  targets: ["MyServer"]),
          ],
          dependencies: [
              .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.0.0")),
          ],
          targets: [
              .executableTarget(
                  name: "MyServer",
                  dependencies: [
                      .product(name: "Vapor", package: "vapor")
                  ]),
              .testTarget(
                  name: "MyServerTests",
                  dependencies: ["MyServer"]),
          ]
      )
    • 2:00 - Simple, server code

      import Vapor
      
      @main
      public struct MyServer {
          public static func main() async throws {
              let webapp = Application()
              webapp.get("greet", use: Self.greet)
              webapp.post("echo", use: Self.echo)
              try webapp.run()
          }
      
          static func greet(request: Request) async throws -> String {
              return "Hello from Swift Server"
          }
      
          static func echo(request: Request) async throws -> String {
              if let body = request.body.string {
                  return body
              }
              return ""
          }
      }
    • 3:42 - Using curl to test the local server

      curl http://127.0.0.1:8080/greet; echo
      curl http://127.0.0.1:8080/echo --data "Hello from WWDC 2022"; echo
    • 4:10 - Simple, iOS app server abstraction

      import Foundation
      
      struct MyServerClient {
          let baseURL = URL(string: "http://127.0.0.1:8080")!
      
          func greet() async throws -> String {
              let url = baseURL.appendingPathComponent("greet")
              let (data, _) = try await URLSession.shared.data(for: URLRequest(url: url))
              guard let responseBody = String(data: data, encoding: .utf8) else {
                  throw Errors.invalidResponseEncoding
              }
              return responseBody
          }
      
          enum Errors: Error {
              case invalidResponseEncoding
          }
      }
    • 5:00 - Simple, iOS app server call SwiftUI integration

      import SwiftUI
      
      struct ContentView: View {
          @State var serverGreeting = ""
          var body: some View {
              Text(serverGreeting)
                  .padding()
                  .task {
                      do {
                          let myServerClient = MyServerClient()
                          self.serverGreeting = try await myServerClient.greet()
                      } catch {
                          self.serverGreeting = String(describing: error)
                      }
                  }
          }
      }
      
      struct ContentView_Previews: PreviewProvider {
          static var previews: some View {
              ContentView()
          }
      }
    • 9:51 - Food truck, basic server

      import Foundation
      import Vapor
      
      @main
      struct FoodTruckServerBootstrap {
          public static func main() async throws {
              // initialize the server
              let foodTruckServer = FoodTruckServer()
              // initialize the web framework and configure the http routes
              let webapp = Application()
              webapp.get("donuts", use: foodTruckServer.listDonuts)
              try webapp.run()
          }
      }
      
      struct FoodTruckServer {
          private let storage = Storage()
      
          func listDonuts(request: Request) async -> Response.Donuts {
              let donuts = self.storage.listDonuts()
              return Response.Donuts(donuts: donuts)
          }
      
          enum Response {
              struct Donuts: Content {
                  var donuts: [Model.Donut]
              }
          }
      }
      
      struct Storage {
          var donuts = [Model.Donut]()
      
          func listDonuts() -> [Model.Donut] {
              return self.donuts
          }
      }
      
      enum Model {
          struct Donut: Codable {
              var id: Int
              var name: String
              var date: Date
              var dough: Dough
              var glaze: Glaze?
              var topping: Topping?
          }
      
          struct Dough: Codable {
              var name: String
              var description: String
              var flavors: FlavorProfile
          }
      
          struct Glaze: Codable {
              var name: String
              var description: String
              var flavors: FlavorProfile
          }
      
          struct Topping: Codable {
              var name: String
              var description: String
              var flavors: FlavorProfile
          }
      
          public struct FlavorProfile: Codable {
              var salty: Int?
              var sweet: Int?
              var bitter: Int?
              var sour: Int?
              var savory: Int?
              var spicy: Int?
          }
      }
    • 12:18 - Food truck, server donuts menu

      [
        {
            "id": 0,
            "name": "Deep Space",
            "date": "2022-04-20T00:00:00Z",
            "dough": {
                "name": "Space Strawberry",
                "description": "The Space Strawberry plant grows its fruit as ready-to-pick donut dough.",
                "flavors": {
                    "sweet": 3,
                    "savory": 2
                }
            },
            "glaze": {
                "name": "Delta Quadrant Slice",
                "description": "Locally sourced, wormhole-to-table slice of the delta quadrant of the galaxy. Now with less hydrogen!",
                "flavors": {
                    "salty": 1,
                    "sour": 3,
                    "spicy": 1
                }
            },
            "topping": {
                "name": "Rainbow Sprinkles",
                "description": "Cultivated from the many naturally occurring rainbows on various ocean planets.",
                "flavors": {
                    "salty": 2,
                    "sweet": 2,
                    "sour": 1
                }
            }
        },
        {
            "id": 1,
            "name": "Chocolate II",
            "date": "2022-04-20T00:00:00Z",
            "dough": {
                "name": "Chocolate II",
                "description": "When Harold Chocolate II discovered this substance in 3028, it finally unlocked the ability of interstellar travel.",
                "flavors": {
                    "salty": 1,
                    "sweet": 3,
                    "bitter": 1,
                    "sour": -1,
                    "savory": 1
                }
            },
            "glaze": {
                "name": "Chocolate II",
                "description": "A thin layer of melted Chocolate II, flash frozen to fit the standard Space Donut shape. Also useful for cleaning starship engines.",
                "flavors": {
                    "salty": 1,
                    "sweet": 2,
                    "bitter": 1,
                    "sour": -1,
                    "savory": 2
                }
            },
            "topping": {
                "name": "Chocolate II",
                "description": "Particles of Chocolate II moulded into a sprinkle fashion. Do not feed to space whales.",
                "flavors": {
                    "salty": 1,
                    "sweet": 2,
                    "bitter": 1,
                    "sour": -1,
                    "savory": 2
                }
            }
        },
        {
            "id": 2,
            "name": "Coffee Caramel",
            "date": "2022-04-20T00:00:00Z",
            "dough": {
                "name": "Hardened Coffee",
                "description": "Unlike other donut sellers, our coffee dough is simply a lot of coffee compressed into an ultra dense torus.",
                "flavors": {
                    "sweet": -2,
                    "bitter": 4,
                    "sour": 2,
                    "spicy": 1
                }
            },
            "glaze": {
                "name": "Caramel",
                "description": "Some good old fashioned Earth caramel.",
                "flavors": {
                    "salty": 2,
                    "sweet": 3,
                    "sour": -1,
                    "savory": 1
                }
            },
            "topping": {
                "name": "Nebula Bits",
                "description": "Scooped up by starships traveling through a sugar nebula.",
                "flavors": {
                    "sweet": 4,
                    "spicy": 1
                }
            }
        }
      ]
    • 12:23 - Food truck, server package manifest

      // swift-tools-version: 5.7
      
      import PackageDescription
      
      let package = Package(
          name: "FoodTruckServer",
          platforms: [.macOS("12.0")],
          products: [
              .executable(
                  name: "FoodTruckServer",
                  targets: ["FoodTruckServer"]),
          ],
          dependencies: [
              .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.0.0")),
          ],
          targets: [
              .executableTarget(
                  name: "FoodTruckServer",
                  dependencies: [
                      .product(name: "Vapor", package: "vapor")
                  ],
                  resources: [
                      .copy("menu.json")
                  ]
              ),
              .testTarget(
                  name: "FoodTruckServerTests",
                  dependencies: ["FoodTruckServer"]),
          ]
      )
    • 12:30 - Food truck, server with integrated storage

      import Foundation
      import Vapor
      
      @main
      struct FoodTruckServerBootstrap {
          public static func main() async throws {
              // initialize the server
              let foodTruckServer = FoodTruckServer()
              try await foodTruckServer.bootstrap()
              // initialize the web framework and configure the http routes
              let webapp = Application()
              webapp.get("donuts", use: foodTruckServer.listDonuts)
              try webapp.run()
          }
      }
      
      struct FoodTruckServer {
          private let storage = Storage()
      
          func bootstrap() async throws {
              try await self.storage.load()
          }
      
          func listDonuts(request: Request) async -> Response.Donuts {
              let donuts = await self.storage.listDonuts()
              return Response.Donuts(donuts: donuts)
          }
      
          enum Response {
              struct Donuts: Content {
                  var donuts: [Model.Donut]
              }
          }
      }
      
      actor Storage {
          let jsonDecoder: JSONDecoder
          var donuts = [Model.Donut]()
      
          init() {
              self.jsonDecoder = JSONDecoder()
              self.jsonDecoder.dateDecodingStrategy = .iso8601
          }
      
          func load() throws {
              guard let path = Bundle.module.path(forResource: "menu", ofType: "json") else {
                  throw Errors.menuFileNotFound
              }
              guard let data = FileManager.default.contents(atPath: path) else {
                  throw Errors.failedLoadingMenu
              }
      
              self.donuts = try self.jsonDecoder.decode([Model.Donut].self, from: data)
          }
      
          func listDonuts() -> [Model.Donut] {
              return self.donuts
          }
      
          enum Errors: Error {
              case menuFileNotFound
              case failedLoadingMenu
          }
      }
      
      enum Model {
          struct Donut: Codable {
              var id: Int
              var name: String
              var date: Date
              var dough: Dough
              var glaze: Glaze?
              var topping: Topping?
          }
      
          struct Dough: Codable {
              var name: String
              var description: String
              var flavors: FlavorProfile
          }
      
          struct Glaze: Codable {
              var name: String
              var description: String
              var flavors: FlavorProfile
          }
      
          struct Topping: Codable {
              var name: String
              var description: String
              var flavors: FlavorProfile
          }
      
          public struct FlavorProfile: Codable {
              var salty: Int?
              var sweet: Int?
              var bitter: Int?
              var sour: Int?
              var savory: Int?
              var spicy: Int?
          }
      }
    • 14:42 - Using curl and jq to test the local server

      curl http://127.0.0.1:8080/donuts | jq .
      curl http://127.0.0.1:8080/donuts | jq '.donuts[] .name'

Developer Footer

  • Vidéos
  • WWDC22
  • Use Xcode for server-side development
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines