Bird
0
0

How can you modify a URLSession to add custom headers to every request?

hard📝 Application Q9 of 15
iOS Swift - Networking
How can you modify a URLSession to add custom headers to every request?
AAdd headers directly to <code>URLSession.shared</code>
BModify the URL string to include headers
CSet headers inside the completion handler of dataTask
DCreate a <code>URLSessionConfiguration</code>, set <code>httpAdditionalHeaders</code>, then create a session with it
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to add headers globally

    Custom headers are added by configuring URLSessionConfiguration and setting httpAdditionalHeaders.
  2. Step 2: Create a session with the configuration

    Use the configuration to create a new URLSession instance that applies headers to all requests.
  3. Final Answer:

    Create a URLSessionConfiguration, set httpAdditionalHeaders, then create a session with it -> Option D
  4. Quick Check:

    Custom headers = URLSessionConfiguration + httpAdditionalHeaders [OK]
Quick Trick: Use URLSessionConfiguration to add headers globally [OK]
Common Mistakes:
  • Trying to add headers directly to shared session
  • Setting headers inside completion handler
  • Appending headers to URL string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes