I’m attempting to obtain a picture in Goal C from a CDN that serves photographs. This works fantastic if the picture URL is identical because the URL that was requested. However some photographs will lead to a redirect to a unique URL (even a unique area). For instance, this could be the request URL however then the CDN will redirect to the second URL.
If viewing this in a browser or different context, it really works fantastic (the URL will get up to date, however the picture is proven appropriately). However when attempting to obtain it utilizing an NSURLSessionDownloadTask, it leads to a 404 standing code.
Right here is the code I’m utilizing to carry out the obtain. The code does work if the picture will not be redirected (the URL of the picture that’s downloaded is identical because the picture URL that was requested). However in any other case, if the server does carry out any redirection, this leads to a 404 standing.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
configuration.URLCache = nil;
configuration.timeoutIntervalForRequest = 5 * 60;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:key.url];
NSURLSessionDownloadTask *job = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSInteger statusCode = 0;
NSHTTPURLResponse *httpResponse = nil;
if ([response isKindOfClass:NSHTTPURLResponse.class]) {
httpResponse = (NSHTTPURLResponse *)response;
statusCode = httpResponse.statusCode;
} else {
statusCode = error.code;
}
// the standing code is 404 when the server points a redirect
}];
[task resume];
Does anybody know what I may do to get this to work? If I request a picture from URL1 however it’s served again at URL2, then I’d simply love to do the obtain anyway. It does not matter if the server redirects to a unique URL or not, I’d nonetheless wish to get the picture downloaded.