I have a UITableView that I’m trying to populate with photos from a server along other data (e.g. photo comments). The table cells on the UITableView has a UIImageView that is set with setImageWithURL:placeHolderImage:. The problem is that only the placeholder image is being shown.
I have subclassed AFHTTPClient and I’m using Basic Authentication with my server. So, I am doing something like this:
[self setAuthorizationHeaderWithUsername:@"user" password:@"pass"];
When I do a GET request against the server, I’m getting the JSON that contains the data and the path of the images. Within the tableView:cellForRowAtIndexPath: method, I’m trying to set the UIImageView with setImageWithURL:placeHolderImage:, but each of the requests to download the images is returning a 401 Authorization Required.
I’m assuming that each call to the image URLs does not include the Basic Authentication info that I have in the subclassed AFHTTPClient. If that is indeed the case, is there a way to have the setImageWithURL:placeHolderImage: use the subclassed AFHTTPClient that contains the Basic Authentication info? Or is there some other approach I can take to respect this Basic Auth in each call to download the images?
While the
setImageWithURL:placeHolderImage:is a super convenient method you may have to use some work arounds to get this to work as you want it too. Since image downloads don’t usually require authentication this isn’t too surprising. You could alter the code ofUIImageView+AFNetworking.m. This way you could use thesetAuthorizationHeaderWithUsernamemethod to authorize yourself.Alternatively, you could utilize the underlying methods that
setImageWithURL:placeHolderImage:uses here and create your own similar class usingAFImageRequestOperationin which you could set an appropriate authorization header. If you choose this be sure to implement similar cache methods to make everything as efficient as possible.