我尝试使用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
,但该块永远不会被调用。
所以问题是:如何使用AFURLSessionManager
传递基本http身份验证的凭据?
答案 0 :(得分:0)
我修复了它:您需要使用-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((originalImage), 1.0)];
NSUInteger length = imageData.length;
[[self fileSizeLabel] setText:[NSString stringWithFormat:@"File Size : %@",[NSByteCountFormatter stringFromByteCount:imageData.length countStyle:NSByteCountFormatterCountStyleFile]]];
[self dismissViewControllerAnimated:YES completion:nil];
}
来处理下载任务的身份验证问题。