Ask Your Question
2

How to set custom HTTP headers with SDWebImage?

asked 2022-08-14 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-13 15:00:00 +0000

scrum gravatar image

To set custom HTTP headers with SDWebImage, you need to use the SDWebImageDownloader class to customize the download operations. Here's an example:

// Create a custom NSURLSessionConfiguration
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.HTTPAdditionalHeaders = @{ @"Authorization": @"Token myToken" };

// Create a custom downloader using the configuration
SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] initWithSessionConfiguration:configuration];

// Use the downloader to load the image with the custom headers
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
             placeholderImage:nil
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        // Completion block code here
                    }];

In this example, we create a custom NSURLSessionConfiguration that includes an Authorization header with a token value. We then create a custom SDWebImageDownloader using this configuration and use it to load the image. The custom headers will be sent with the HTTP request for the image.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-14 11:00:00 +0000

Seen: 17 times

Last updated: May 13 '22