Article

Making Default and Ephemeral Requests

Send requests from your app when it is running in the foreground.

Overview

Use default and ephemeral requests when your app is running in the foreground.

Request Time-Sensitive Information

To request time-sensitive information:

  1. Access the shared, default URL session object using the URLSession class’s shared property.

  2. Create a task to request the data by calling the session object’s dataTask(with:completionHandler:) method.

  3. Start the task by calling its resume() method.

The task returns the requested data (or any errors) to your completion handler.

You can further customize your request by providing URLSessionConfiguration or NSURLRequest objects, or by requesting a different type of task (for example, a download or upload task). Whenever possible, use a single, shared session for all your requests. Creating and invalidating sessions adds unnecessary overhead.

Request Sensitive or Private Information

Ephemeral sessions are similar to default URL sessions; however, ephemeral sessions don’t store caches, credential stores, or any session-related data to disk. Because an ephemeral session does not save potentially sensitive data, it is ideal for private browsing or other privacy-related tasks. For more information, see ephemeral.

To request sensitive or private information:

  1. Access an ephemeral configuration using the URLSessionConfiguration class’s ephemeral property.

  2. Create an ephemeral session by calling the URLSession class’s init(configuration:) initializer, and passing the ephemeral configuration.

  3. Create a task to request the data by calling the session object’s dataTask(with:completionHandler:) method.

  4. Start the task by calling its resume() method.

The task returns the requested data (or any errors) to your completion handler.

Indicate When Requests Can Be Deferred

By default, URL session tasks fail immediately whenever connectivity is unavailable. The system returns an error, such as NSURLErrorNotConnectedToInternet.

Setting the waitsForConnectivity property to true tells the system to wait for connectivity before starting the task. This prevents the requests from failing if the watch cannot currently connect to the network. You can use the urlSession(_:taskIsWaitingForConnectivity:) delegate method to learn when a request is deferred.

To create a deferrable request:

  1. Access a default configuration using the URLSessionConfiguration class’s default property.

  2. Set the configuration’s waitsForConnectivity property to true.

  3. Create a session by calling the URLSession class’s init(configuration:) initializer, and passing the configuration.

  4. Create a task to request the data by calling the session object’s dataTask(with:completionHandler:) method.

  5. Start the task by calling its resume() method.

Clean Up Outstanding Tasks

Keep track of the URL session tasks you create, and clean them up when your app goes to the background.

To clean up outstanding tasks:

  1. In your extension delegate’s applicationWillResignActive() method, cancel any outstanding tasks by calling the URLSessionTask object’s cancel() method.

  2. (Optional) Use a background session to recreate and restart the task. This runs the task in the background, ensuring your app receives a response, even if the app is terminated.

See Also

Making Network Requests

Making Background Requests

Send requests from your app when it is running in the background.