AFNetworking 2.0: NSURLSessionDownloadTask is canc

2019-04-11 05:56发布

问题:

I'm using a simple example from the README file (from AFNetworking 2.0 page) to download an image. All works fine for the images served directly from my web-sever. But for those located on the S3 the web-server returns 302 Redirect the following error:

Error Domain=NSURLErrorDomain Code=-999 "cancelled"

When I try to download the image (using the same code) directly from the S3 (omitting the redirection) I receive the same error.

I've checked the availability of the actual image with wget. The image is publicly available (https://youguide.s3.amazonaws.com/icons/user_96x96.png).

Anyone experience the similar problem?

回答1:

I was having the same problem with S3 and seem to have resolved it. AFNetworking was returning a value in the authentication challenge handling logic that was canceling the request.

By telling the AFURLSessionManager how to handle authentication challenges I've fixed the problem.

[manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
    return NSURLSessionAuthChallengePerformDefaultHandling;
}];