Ad

Why Do I See An Error About 'data(for:delegate:) Is Only Available On IOS 15.0+' Even Though Modern Concurrency Is Backward Compatible

- 1 answer

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?

Ad

Answer

As stated in this Swift by Sundell article:

Although Swift 5.5’s new concurrency system is becoming backward compatible in Xcode 13.2, some of the built-in system APIs that make use of these new concurrency features are still only available on iOS 15, macOS Monterey, and the rest of Apple’s 2021 operating systems.

This is how John replicates an async/await-powered URLSession API for one method:

@available(iOS, deprecated: 15.0, message: "Use the built-in API instead")
extension URLSession {
    func data(from url: URL) async throws -> (Data, URLResponse) {
         try await withCheckedThrowingContinuation { continuation in
            let task = self.dataTask(with: url) { data, response, error in
                 guard let data = data, let response = response else {
                     let error = error ?? URLError(.badServerResponse)
                     return continuation.resume(throwing: error)
                 }

                 continuation.resume(returning: (data, response))
             }

             task.resume()
        }
    }
}
Ad
source: stackoverflow.com
Ad