Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect handler. #1072

Merged
merged 1 commit into from
Dec 17, 2018
Merged

Conversation

devuarpd
Copy link

Example of use:

  1. Implement ImageDownloadRedirectHandler protocol:

extension NetworkManager: ImageDownloadRedirectHandler
{
func handleHTTPRedirection(for _: SessionDataTask, response _: HTTPURLResponse, newRequest: URLRequest, completionHandler: @escaping (URLRequest?) -> Void)
{
var modified = newRequest
modified.allHTTPHeaderFields = authorizationHeaders
completionHandler(modified)
}
}

  1. Pass redirect handler as .redirectHandler option:

imageView.kf.setImage(with: url, placeholder: placeholder, options: [ .redirectHandler(NetworkManager.shared)])

@onevcat
Copy link
Owner

onevcat commented Dec 17, 2018

LGTM!

@onevcat onevcat merged commit 33ae9e0 into onevcat:master Dec 17, 2018
@oney
Copy link

oney commented Apr 23, 2019

@devuarpd can you give more detailed explanation about how to create NetworkManager?
Thanks!

@devuarpd
Copy link
Author

@oney
Redirection handler needed for modification of request during redirection. In my way I need to attach token to authorization headers:
extension NetworkManager: ImageDownloadRedirectHandler
{
func handleHTTPRedirection(for _: SessionDataTask, response _: HTTPURLResponse, newRequest: URLRequest, completionHandler: @escaping (URLRequest?) -> Void)
{
var modified = newRequest
modified.allHTTPHeaderFields = authorizationHeaders
completionHandler(modified)
}
}

If you see I change default authorization headers onto my authorization headers which contain needed token.

skoduricg pushed a commit to rentpath/Kingfisher that referenced this pull request Sep 24, 2021
@aehlke
Copy link

aehlke commented Jul 5, 2022

How do I use this in SwiftUI?

@onevcat
Copy link
Owner

onevcat commented Jul 7, 2022

You can use either of the redirectHandler(_:) modifier:

截屏2022-07-07 10 13 02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants