🌐

Understanding URL Session Configuration

Dec 16, 2024

URL Session Configuration

Overview

URL Session Configuration allows control over:

  • Network service type
    • Timeouts
    • Caching
    • HTTP headers
  • Default configuration uses a persistent disk-based cache and stores credentials in the user’s keychain.
  • Different types of configurations:
    • Default Configuration: Standard settings, stores data persistently.
    • Ephemeral Configuration: Does not store cookies, credentials, or cache data to disk.
    • Background Configuration: Allows data transfer when the app runs in the background and requires an identifier.

Configuration Properties

  • Configurations allow customization of several properties before creating a URL session, such as:
    • Cellular access
    • Request timeouts
    • Cookie acceptance
    • Waiting for connectivity
  • Changes to configuration must be made before session creation; subsequent changes have no effect.

Example Walkthrough in Playground

  • To create a session configuration:
    • Access the shared singleton session object.
    • Configuration properties become read-only after session creation.
  • Changing Properties Example:
    • Set allowCellularAccess to false.
    • Create configuration object via static property on URLSessionConfiguration.
    • For background configurations, provide an identifier for session reconstruction.

Best Practices

  • Allow Cellular Access: Can be set to false as needed.
  • Waits for Connectivity: Apple recommends setting to true for all non-background configurations.
  • Creating a new session with custom configurations ensures it uses specified settings.
  • Default session creation uses default property values without modification.

Additional Resources

Tessa’s siegsgdiegdj d d she’s da

  • Check NSURLSessionConfiguration documentation for more details on configuration options and properties.