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
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Use async/await with URLSession

    Discover how you can adopt Swift concurrency in URLSession using async/await and AsyncSequence, and how you can apply Swift concurrency concepts to improve your networking code.

    Recursos

    • URLSession
      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC22

    • Eliminate data races using Swift Concurrency

    WWDC21

    • Accelerate networking with HTTP/3 and QUIC
    • Analyze HTTP traffic in Instruments
    • Meet async/await in Swift
    • Meet AsyncSequence
  • Buscar neste vídeo...
    • 2:52 - Fetch photo with async/await

      // Fetch photo with async/await
      
      func fetchPhoto(url: URL) async throws -> UIImage
      {
          let (data, response) = try await URLSession.shared.data(from: url)
      
          guard let httpResponse = response as? HTTPURLResponse,
                httpResponse.statusCode == 200 else {
              throw WoofError.invalidServerResponse
          }
      
          guard let image = UIImage(data: data) else {
              throw WoofError.unsupportedImage
          }
      
          return image
      }
    • 3:45 - URLSession.data

      let (data, response) = try await URLSession.shared.data(from: url)
      guard let httpResponse = response as? HTTPURLResponse,
            httpResponse.statusCode == 200 /* OK */ else {
          throw MyNetworkingError.invalidServerResponse
      }
    • 4:03 - URLSession.upload

      var request = URLRequest(url: url)
      request.httpMethod = "POST"
      
      let (data, response) = try await URLSession.shared.upload(for: request, fromFile: fileURL)
      guard let httpResponse = response as? HTTPURLResponse,
            httpResponse.statusCode == 201 /* Created */ else {
          throw MyNetworkingError.invalidServerResponse
      }
    • 4:21 - URLSession.download

      let (location, response) = try await URLSession.shared.download(from: url)
      guard let httpResponse = response as? HTTPURLResponse,
            httpResponse.statusCode == 200 /* OK */ else {
          throw MyNetworkingError.invalidServerResponse
      }
      
      try FileManager.default.moveItem(at: location, to: newLocation)
    • 4:44 - Cancellation

      let task = Task {
          let (data1, response1) = try await URLSession.shared.data(from: url1)
      
          let (data2, response2) = try await URLSession.shared.data(from: url2)
      
      }
      
      task.cancel()
    • 7:53 - asyncSequence demo

      let (bytes, response) = try await URLSession.shared.bytes(from: Self.eventStreamURL)
      guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
          throw WoofError.invalidServerResponse
      }
      for try await line in bytes.lines {
          let photoMetadata = try JSONDecoder().decode(PhotoMetadata.self, from: Data(line.utf8))
          await updateFavoriteCount(with: photoMetadata)
      }
    • 11:20 - task specific delegate demo

      class AuthenticationDelegate: NSObject, URLSessionTaskDelegate {
          private let signInController: SignInController
          
          init(signInController: SignInController) {
              self.signInController = signInController
          }
          
          func urlSession(_ session: URLSession,
                          task: URLSessionTask,
                          didReceive challenge: URLAuthenticationChallenge) async
          -> (URLSession.AuthChallengeDisposition, URLCredential?) {
              if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic {
                  do {
                      let (username, password) = try await signInController.promptForCredential()
                      return (.useCredential,
                              URLCredential(user: username, password: password, persistence: .forSession))
                  } catch {
                      return (.cancelAuthenticationChallenge, nil)
                  }
              } else {
                  return (.performDefaultHandling, nil)
              }
          }
      }

Developer Footer

  • Vídeos
  • WWDC21
  • Use async/await with URLSession
  • 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