URLSession async in iOS 13 and 14

URLSession async in iOS 13 and 14

Swift 5.5 has initially introduced async/await for iOS 15.
With a very welcome news, Xcode 13.2 has extended support of async/await to previous versions iOS 13 and 14.

This extension of async/await doesn't include support for the libraries.
You can find explained in multiple blog posts, for example Making async system APIs backward compatible of John Sundell (and its realated Swift Package AsyncCompatibilityKit).

Essentially it is necessary to re-implement these basic functionalities.
In my case, I have found the previous blog post of John very well explained, but not complete enough for the need of my SwiftDownloader sample.

I have found a complete implementation in the Swift package ConcurrencyCompatibility of Zachary Waldowski.

So to upgrade my project to support iOS 14 have done the following:

  • from the project node, right click, Add Package:
    URLSession---Add-package
  • in the next dialog, insert the package url, https://github.com/zwaldowski/ConcurrencyCompatibility
    URLSession---Package-Dialog
  • in case you want to review the packages you are using, or you want to remove unnecessary packages, you can find them in the project settings, in the Package Dependencies tab:
    URLSession---Package-Info
  • modify the code that was using the URLSession APIs with the wrappers defined in ConcurrencyCompatibility package (they use the iOS 15+ APIs when possible, and the re-implementation on the previous iOS versions)
  • finally, update the project project to have iOS 14 as minimum version (it's not possible to downgrade to iOS 13 because of other features not available).

You can find the updated project in GitHub at SwiftDownloader.
Litte update: as I continue development of this project following the latest iOS releases, I have tagged this work with iOS_13_14.