SDWebImage and setting custom HTTP headers?
Asked Answered
H

6

23

I´ve just changed my code for caching images away from EGOImageCache to SDWebView. Unfortunately i don´t know how to set custom HTTP headers as i have to send authentification to be able to fetch images. It was easy done with EGOImageCache as i´ve extended the NSURLRequest at the appropriate place. But i don´t know how to do that with the SDWebView.framework. I see the headers and i´ve found methods in SDWebImageDownloader.h containing

    /**
 * Set a value for a HTTP header to be appended to each download HTTP request.
 *
 * @param value The value for the header field. Use `nil` value to remove the header.
 * @param field The name of the header field to set.
 */
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

/**
 * Returns the value of the specified HTTP header field.
 *
 * @return The value associated with the header field field, or `nil` if there is no corresponding header field.
 */
- (NSString *)valueForHTTPHeaderField:(NSString *)field;

It seems that the lib does support HTTP headers. But as i use UIImageView+WebCache.h i can´t see there an option for setting the headers. In my code i call

[self.imageView setImageWithURL:[NSURL URLWithString:themeImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

Can anybody tell me how to set HTTP headers?

Hamhung answered 28/4, 2013 at 18:50 Comment(0)
F
47

I had the same problem, and I tried to make:

SDWebImageDownloader *manager = [SDWebImageDownloader sharedDownloader];
[manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];

But the header were not send. After some tries, I came across the problem, SDWebImageDownloader at sharedDownloader makes a new instance of SDWebImageDownloader, so when you put the header at that instance, the instance that really downloads the image don't has the header.

I've solved making this:

SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
[manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];
Forwent answered 23/5, 2013 at 8:22 Comment(4)
i´ll try that out. thank you for your reply! it seems not a lot developers need that feature.Hamhung
You sir deserve more upvotes. This answer is 100% correct and the original code is contra-intuitive.Beguin
But how do you set the password?Haffner
@9000 What type of password? A http password? Or a normal header? In the last case, you can set multiple headers one for the username, and one for the password (you must call twice the setValue:forHTTPHeaderFiled).Pascual
M
4

I know it's pretty old but couldn't help to share what worked for me. I needed to set a login token value for header logintoken. So, this piece of code did what I wanted -

NSString *loginToken = // Some method to fetch login token    
[SDWebImageDownloader.sharedDownloader setValue:loginToken forHTTPHeaderField:@"logintoken"];
Marrowfat answered 30/7, 2015 at 21:16 Comment(0)
S
4

Swift Version

let imageDownloader = SDWebImageDownloader.shared()
imageDownloader.setValue("Username", forHTTPHeaderField: "X-Oauth-Username")
Shuffle answered 13/11, 2017 at 13:24 Comment(1)
This is the perfect answer.Jeanajeanbaptiste
W
3

I am using Basic authentication and setting the username and password on the sharedDownloader helped:

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
downloader.username = @"username";
downloader.password = @"password";
Wing answered 27/11, 2016 at 20:5 Comment(0)
P
2

Swift 4.1

let manager = SDWebImageManager.shared().imageDownloader
manager?.setValue("oAuthToken",forHTTPHeaderField: "AuthHeaderName")
manager?.downloadImage(with: imageURL, options: SDWebImageDownloaderOptions.useNSURLCache, progress:
                { (receivedSize, expectedSize , url) in
                // progression tracking code
            }, completed: { (image,data , error,finished) in
                if error == nil && image != nil {
                    // here the downloaded image is cached, now you need to set it to the imageView
                    DispatchQueue.main.async {
                        imageView.image = image
                        self.maskCircle(anyImage: image!)
                    }
                } else {
                    // handle the failure
                    DispatchQueue.main.async {
                        let defaultImage = UIImage(named: "defaultImage")
                        imageView.image = defImage
                        self.maskCircle(anyImage: defImage)
                    }
                }
            })
Peterson answered 21/8, 2018 at 11:11 Comment(0)
W
1

SDWebImage 5 onward need to used following snipped code.

    func configureSDWebImageHeader(){
    let requestModifier = SDWebImageDownloaderRequestModifier { (request) -> URLRequest? in
        var mutableRequest = request
        // Used Your Header Keys
        mutableRequest.setValue("", forHTTPHeaderField: KeyConstant.USERID)
        mutableRequest.setValue("", forHTTPHeaderField: KeyConstant.AUTH_KEY)
        return mutableRequest
    };
    SDWebImageDownloader.shared.requestModifier = requestModifier
}
Watchmaker answered 29/3, 2023 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.