Modern concurrency with the new Async / Await
was introduced for iOS 15 and above with Swift 5.5 but very soon, with the release of Xcode 13.2 (and subsequently 13.2.1) it enabled us to use Async
and Await
to develop for iOS 13+, macOS 10.15+ etc. However, when I try to make an asynchronous request like this:
let (data, response) = try await URLSession.shared.data(for: request)
It does not run on iOS 13+. Instead, I get an error stating:
data(for:delegate:)
is only available in iOS 15.0 or newer
The error goes away when I set minimum deployment target to iOS 15.0, but I want the software to support iOS 13.0+. I understand that data(for:delegate:)
is supported only on iOS 15.0+, but what is the point of backward compatibility to 13.0+, if I am not able to make an asynchronous network fetch request?
-
data(for:delegate:) documentation -> which states minimum iOS required is 15.0
-
Xcode release notes -> where they mentioned a clang bug-fix to deploying Swift concurrency features to iOS prior to 15
2
Answers
As stated in this Swift by Sundell article:
This is how John replicates an async/await-powered URLSession API for one method:
Here is another variant that can handle URL requests instead of just URLs: