iosswifturlsessionxcode15swift-concurrency

URLSession - 'data(from:)' was deprecated in iOS 15.0


I'm using URLSession in concurrent context, the code I run is very simple (see below), but it's producing strange warning:

// WARNING: 'data(from:)' was deprecated in iOS 15.0: Use the built-in API instead
let (data, response) = try await URLSession.shared.data(from: assetURL)

I'm curious what this warning may mean since official reference page on data(from:delegate:) doesn't mention any deprecations. This API is relatively new.

Environment:


Solution

  • We can infer that you must be using an extension inspired by Making async system APIs backward compatible:

    @available(iOS, deprecated: 15.0, message: "Use the built-in API instead")
    extension URLSession {
        func data(from url: URL) async throws -> (Data, URLResponse) {
            …
        }
    }
    

    The custom deprecation message is identical, which is unlikely to be a coincidence.

    That extension is intended to provide backward-compatibility for iOS 13 or 14, but as you are targeting iOS 15 and later, it is unnecessary (and just confusing).

    You can control-click on the line of code producing this warning and choose “Jump to Definition” and you should be taken to the offending implementation. You can either remove this extension, or, worst case, if this is buried in some third-party package that you cannot easily modify, call it like so, to remove the ambiguity, and explicitly call the native URLSession method:

    let (data, response) = try await URLSession.shared.data(from: assetURL, delegate: nil)