HTTP/3 support for URLSession

HTTP/3 support for URLSession

Β·

2 min read

HTTP/3 uses QUIC, a multiplexed transport protocol built on UDP. Do you need to change anything for your iOS apps when using URLSession?

The quick answer is: No, HTTP3 support is enabled by default in URLSession.

WWDC 2021 session Accelerate networking with HTTP/3 and QUIC gives you the following statement:

If you're using URLSession, you don't need to change your app since iOS 15 and macOS Monterey ship with HTTP/3 enabled by default. Once you enable HTTP/3 on your server, you're good to go.

Apple's Technote TN3102: HTTP/3 in your app does not give you such a simple answer, but it provides sample code and instructions on how you can verify that HTTP/3 was actually used.

The Technote also goes into detail on how URLSession may discover to use HTTP/3.

Service discovery for HTTP/3 is performed in one of the following ways:

  • The recommended approach is to configure your DNS server to advertise the HTTPS resource record for alpn="h3,h2".

  • Alternatively, configure your server to respond back with the Alt-Svc header that advertises HTTP/3. For example, Alt-Srv: h3=":443"; ma=2592000.

URLSession won't use HTTP/3 unless it was advertised.

Hence you might observe that the very first connection does not use HTTP/3 and only for subsequent connections.

To attempt to use HTTP/3 on the first transaction, you can set the assumesHTTP3Capable property on the URLRequest before passing it to a data task in URLSession.

//var request: URLRequest = ...
request.assumesHTTP3Capable = true

Networks may still block HTTP/3, or your server may not support HTTP/3. In that case, Apple will fall back to HTTP/2.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!