How to get the final redirected url of a website/url?

How can I get the final redirected online url? I was thinking about using an URLSession data or download task.

URLSession has two ways to deal with this:

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes

Thanks. I'm now using the response of a data task:

    func redirectedURL(for url: URL, completionHandler: @escaping (URL?, Error?) -> Void) {
        let request = URLRequest(url: url)
        URLSession.shared.dataTask(with: request, completionHandler: { _, response, error in
            if let httpResponse = response as? HTTPURLResponse, let location = httpResponse.allHeaderFields["Location"] as? String, let locationURL = URL(string: location) {
                completionHandler(locationURL, error)
            }
        })
    }