我正在尝试使用 AFNetworking 在我的 iOS 应用程序中实现一个简单的文件下载方法。首先,我创建一个 AFURLSessionManager
:
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let manager = AFURLSessionManager(sessionConfiguration: configuration)
然后我创建并触发下载任务:
let task = manager.downloadTaskWithRequest(request, progress: nil, destination: {
(targetPath: NSURL, response: NSURLResponse) -> NSURL in
// ...
return fullPathURL
}, completionHandler: {
(response: NSURLResponse, filePath: NSURL?, error: NSError?) in
// ...
})
task.resume()
当我运行该应用程序时,我收到一条错误消息,告诉我我无权访问该网址:
NSLocalizedDescription=Request failed: unauthorized (401)
我尝试使用setSessionDidReceiveAuthenticationChallengeBlock
,但该 block 从未被调用。
所以问题是:如何使用 AFURLSessionManager
传递基本 http 身份验证的凭据?
最佳答案
我修复了它:您需要使用 setTaskDidReceiveAuthenticationChallengeBlock
来处理下载任务的身份验证挑战。
关于ios - AFURLSessionManager 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649456/