Having trouble catching a 'redirect' with URLSessionDownloadDelegate

I've implemented

    func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) 

and

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) 

I've put a breakpoint in each but the BP in willPerformHTTPRedirection never fires.

When the didWriteData fires and I inspect downloadTask.originalRequest I see my original request URL When I inspect downloadTask.currentRequest the returned request contains a different URL.

I'm the farthest thing from an HTTP wizard, but I had thought when originalRequest differs from currentRequest there had been some sort of server-side 'redirection'.

Is there a way for my code to receive a callback when something like this happens? NOTE: my download code works fine, I'm just hoping to detect the case when currentRequest changes. any/all guidance greatly appreciated

on the off chance it helps, are are my original and current request values:

(lldb) po downloadTask.originalRequest
▿ Optional<URLRequest>
  ▿ some : https://audio.listennotes.com/e/p/c524803c1a90412f922948274ecc3625/

(lldb) po downloadTask.currentRequest
▿ Optional<URLRequest>
  ▿ some : https://26973.mc.tritondigital.com:443/OMNY_HAPPIERWITHGRETCHENRUBIN_PODCAST_P/media-session/76cfceb2-1801-4570-b830-ded57611a9cf/d/clips/796469f9-ea34-46a2-8776-ad0f015d6beb/e1b22d0b-6974-4bb8-81ba-b2480119983c/2f35a8ca-b982-44e9-8122-b3dc000ae0e1/audio/direct/t1769587393/Ep_571_Want_to_Join_Us_for_a_No-Spend_February_Plus_a_Better_Word_for_Squats.mp3?t=1769587393&in_playlist=751ada7f-ded3-44b9-bfb8-b2480119985b&utm_source=Podcast
Answered by DTS Engineer in 874163022

Consider the tiny test program at the end of this post. For context, https://apple.com redirects to https://www.apple.com, so this exercises the redirect case. When I run it on my Mac (Xcode 26.2 on macOS 26.2) I see this:

will run task
will redirect
did run task, status: 200, downloaded: file:///…/CFNetworkDownload_WvKFij.tmp

That is, it fetches apple.com, which redirects to www.apple.com, that’s reported to the delegate, and then it continues the download.

IMPORTANT This uses a standard session. If you’re using a background session, the system doesn’t call this delegate method. We even mention that in the docs (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"


import Foundation

final class Delegate: NSObject, URLSessionDownloadDelegate {
    
    func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
        print("will redirect")
        return request
    }

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("did finish downloading")
    }
}

func main() async throws {
    do {
        print("will run task")
        let url = URL(string: "https://apple.com")!
        let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
        let (downloaded, response) = try await URLSession.shared.download(for: request, delegate: Delegate())
        let httpResponse = response as! HTTPURLResponse
        print("did run task, status: \(httpResponse.statusCode), downloaded: \(downloaded)")
    } catch let error as NSError {
        print("did not run task, error: \(error.domain) / \(error.code)")
    }
}

try await main()
Accepted Answer

Consider the tiny test program at the end of this post. For context, https://apple.com redirects to https://www.apple.com, so this exercises the redirect case. When I run it on my Mac (Xcode 26.2 on macOS 26.2) I see this:

will run task
will redirect
did run task, status: 200, downloaded: file:///…/CFNetworkDownload_WvKFij.tmp

That is, it fetches apple.com, which redirects to www.apple.com, that’s reported to the delegate, and then it continues the download.

IMPORTANT This uses a standard session. If you’re using a background session, the system doesn’t call this delegate method. We even mention that in the docs (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"


import Foundation

final class Delegate: NSObject, URLSessionDownloadDelegate {
    
    func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
        print("will redirect")
        return request
    }

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("did finish downloading")
    }
}

func main() async throws {
    do {
        print("will run task")
        let url = URL(string: "https://apple.com")!
        let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
        let (downloaded, response) = try await URLSession.shared.download(for: request, delegate: Delegate())
        let httpResponse = response as! HTTPURLResponse
        print("did run task, status: \(httpResponse.statusCode), downloaded: \(downloaded)")
    } catch let error as NSError {
        print("did not run task, error: \(error.domain) / \(error.code)")
    }
}

try await main()
Having trouble catching a 'redirect' with URLSessionDownloadDelegate
 
 
Q