flocked
(Florian Zand)
1
How can I get the final redirected online url? I was thinking about using an URLSession data or download task.
eskimo
(Quinn “The Eskimo!”)
2
URLSession has two ways to deal with this:
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple
2 Likes
flocked
(Florian Zand)
3
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)
}
})
}